You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1.7 KiB

AdventureCatch

Instructions

Now, we will throw and catch some exceptions.

For the methods attack, takeDamage and heal, if the character is already dead, you must throw a DeadCharacterException. Do this for all the Character subclasses.

The fight method should not throw an exception, so you will need to catch the exception in this method.

Usage

Here is a possible ExerciseRunner.java to test your function :

public class ExerciseRunner {

    public static void main(String[] args) {
        Weapon excalibur = new Weapon("Excalibur", 7);
        Weapon baton = new Weapon("Baton", 3);
        Templar arthur = new Templar("Arthur", 30, 5, 3, excalibur);
        Sorcerer merlin = new Sorcerer("Merlin", 28, 2, baton);

        try {
            arthur.takeDamage(50);
        } catch (DeadCharacterException e) {
            System.out.println(e.getMessage());
        }

        try {
            arthur.takeDamage(2);
        } catch (DeadCharacterException e) {
            System.out.println(e.getMessage());
        }
        try {
            arthur.attack(merlin);
        } catch (DeadCharacterException e) {
            System.out.println(e.getMessage());
        }
    }
}

and its output :

$ javac *.java -d build
$ java -cp build ExerciseRunner 
The templar Arthur is dead.
The templar Arthur is dead.
$ 

Notions

Try
Catch
Declaring thrown
Throwing