Constructor exception exception

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 wolf gets into the constructor, the food value automatically changes to another. I tried using getter-setters , however, I got an unreachable code error. What should I do?

+5
source share
3 answers

The problem with your view is that the throw exception rejection goes beyond the blocks and looks for a try-catch that can 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 the constructor, and the exception leaves the function because there is no try-catch in the constructor for this exception, the construction of this object fails. So, if your Zoo forbids having a wolf as an animal, you should throw an exception (this way 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 a warning to all visitors, you should just show a 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, did you know that your Zoo can only contain one animal and one food?

One more thing. Your findWord(String, String) method is too complicated. Java contains many useful classes and functions, so we do not stop to write code each time again and again. Searching for a substring of a word is really useful and a favorite feature. Take a look at indexOf(String) . It is designed exactly for your purpura and is probably implemented similarly.

+1
source

If you want a specific logic to be executed when a wolf is detected, the exception is the wrong way. You should only throw an exception if the instance design does not work when you find the wolf.

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

Perhaps you need to set the food, int if statement before you throw an exception, and 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; } } 
0
source

All Articles