AlvinL
AlvinL

Reputation: 448

Catching exception in constructor

public class AnimalException extends Exception {
    public AnimalException(String error) {
        super(error);
    }
}

public class Zoo {
    private String animal;
    private String food;

    public Zoo (String animal, String food) throws AnimalException {
        this.animal = animal;
        if (findWord(animal, "wolf")) {
            throw new AnimalException("This animal is a predator.");
            //something ought to be done here, I reckon
        }
        else {
            this.food = food;
        }


    }

    public static boolean findWord(String word, String find) {
        int total = 0;
        int idx = 0;
        while ( (idx = word.indexOf(find, idx))!= -1 ) {
            total++;
            idx++;
        }
        if (total == 0) {
            return false;
        }
        else {
            return true;
        }
}

What I would like to do is when a wolf is caught in the constructor the value for foodis changed automatically to something else. I did try using getter-setters, however, I get the error of unreachable code. What do I do?

Upvotes: 5

Views: 225

Answers (3)

Zereges
Zereges

Reputation: 5209

Problem with your desing is, that caling throw exception leaves the scope of the blocks and is looking for try-catch block which could handle the exception. In your case

void foo() {
    if (somethingbad()) {
        throw new exception();
        bar(); // <-- unreachable code, since throw leaves foo function.
    }
}

If you throw an exception in constructor and the exception leaves the function, because there is no try-catch for that exception in constructor, the construction of that object fails. So, if your Zoo prohibits having a wolf as an animal, you should throw exception (thus Zoo will never be created at all).

public Zoo (String animal, String food) throws AnimalException {
    this.animal = animal;
    if (findWord(animal, "wolf")) {
        throw new AnimalException("This animal is a predator.");
    }
    else {
        this.food = food;
    }
}

void foo() {
    Zoo myZoo;
    try {
        myZoo  = new Zoo("wolf", "meat");
    } catch (AnimalException ex) {
        System.out.println(ex.toString())
    }
    // myZoo is still null, uninitialized.
}

However, if you want to have predators in Zoo, but with warning to all visitors, you should just display some warning.

public Zoo (String animal, String food) throws AnimalException {
    this.animal = animal;
    this.food = food;
    if (findWord(animal, "wolf")) {
        System.out.println("Beware, we have a predator in zoo");
    }
}

Also, are you aware, that your Zoo may contain only one animal and one food?

One more thing. Your method findWord(String, String) is too complicated. Java contains a lot of usefull classes and functions, so that we are not delaying ourselves writing code each time over and over again. Looking for substring of a word is really usefull and favourite function. Take a look at indexOf(String). It is designed exactly for your purpouse and probably implemented similarly.

Upvotes: 1

Eran
Eran

Reputation: 393771

If you want some specific logic to be performed when you detect a "wolf", an exception is not the right way. You should only throw an exception if the construction of the instance should fail when you find a "wolf".

public Zoo (String animal, String food) {
    this.animal = animal;
    if (findWord(animal, "wolf")) { 
        // put whatever logic you wish here
    }
    else {
        this.food = food;
    }
}

Upvotes: 2

Krzysztof Cichocki
Krzysztof Cichocki

Reputation: 6414

Maybe you need to set the food value, int the if statement, before you throw an exception, not after? Like this:

public Object (String animal) throws AnimalException {
        this.animal = animal;
        if (findWord(animal, "wolf")) {
            this.food = food;
            throw new AnimalException("This animal is a predator.");
            //something ought to be done here, I reckon
        }
        else {
            this.food = food;
        }


    }

Upvotes: 0

Related Questions