Originally posted by Rahul Pawar:
Exception
+-- BadTasteException
+-- BitterException
+-- SourException
BadTasteException is defined as an abstract class.
You have a method eatMe that may throw a BitterException or a SourException.
Which of the following method declarations will be acceptable to the
compiler? [Check all correct answers]
A) public void eatMe( Ingredient[] list ) throws BadTasteException
B) public void eatMe( Ingredient[] list ) throws BitterException,
SourException
C) public void eatMe( Ingredient[] list ) may throw BadTasteException
D) public void eatMe( Ingredient[] list )
what will be the output of above programme .
According to me output should be A, B , D .
but the answer is A ,B
can anybody explain me ?
thanks in advance .
rahul
Why d???
When you are defining a new subclass of Exception you are naturally creating a checked exception that means exceptions which must be caught by the Exception Handler in your class.
So the method should be conveying the message that i may be throwing the exception so that where u call the method u put it under a try catch block or else if your try catch is in a method throw it again using 'throws'.
Yah one more thing.Instead of that if you throw Superclass i;e Exception that will do since now you are saying to exception handler handle all the exceoptions of type Exception.That is the case in option 1 where u are saying deal with all the types of BadTasteException.That includes SourTaste.Even u can directly write throws Exception which though is easy to write but a very bad programmming technique unless u dont know which exceptions are going to be thrown
hope that helps
sandeep
[This message has been edited by sandeep bagati (edited March 19, 2001).]