Reputation: 4563
I am new to Java and trying to understand Exceptions in Java.
class MyException extends Exception {
void someMethod () {
doStuff();
}
void doStuff() throws MyException {
try {
throw new MyException();
}
catch(MyException me) {
throw me;
}
}
}
This Program gives the error:
java:3: unreported exception MyException; must be caught or declared to be thrown
doStuff(); ^
The try and catch block are written within the doStuff()
method. Also the doStuff()
method "throws" MyException
, then why do need to throw MyException
in someMethod()
also?
Upvotes: 2
Views: 639
Reputation: 2044
You Need to "rethrow" the Exception from someMethod because MyException is a "Checked Exception" (doesn't extend RuntimeException - which is an "Unchecked Exception") and the compiler forces you to handle Checked Exceptions, like this:
class MyException extends Exception {
/**
* @throws MyException because it is a checked Exception (doesn't extend RuntimeException) and the compiler
* forces you to handle Checked Exceptions.
*/
void someMethod() throws MyException {
doStuff();
}
void doStuff() throws MyException {
try {
throw new MyException();
} catch (MyException me) {
throw me;
}
}
}
Or you need to handle the MyException in some Method so the compiler is happy, like this:
class MyException extends Exception {
void someMethod() {
try {
doStuff();
} catch (MyException ex) {
//Do Something useful with the Exception...
Logger.getLogger(MyException.class.getName()).severe("A Severe Exception Message");
}
}
void doStuff() throws MyException {
try {
throw new MyException();
} catch (MyException me) {
throw me;
}
}
}
Upvotes: 3
Reputation: 5519
Since doStuff() is again throwing the exception. You will have to wrap it around a try catch again.
void someMethod () {
try
{
doStuff();
}
catch ( MyException e )
{
e.printStackTrace();
}
}
Or another option is not to throw the exception again. Everytime you throw an exception it has to be caught.
Upvotes: 2
Reputation: 20631
It doesn't matter what you have in the body of the doStuff() method - because you've declared that it "throws MyException", then the calling method(s) - in this case someMethod() - must either handle the exception (via a try/catch block) or be declared to throw the same exception themselves. In the latter case, the exception if thrown by doStuff() will automatically propagate through to the caller of someMethod().
In other words: the fact that you catch the exception in doStuff() doesn't matter, because doStuff() is declared to throw the exception anyway. You could even remove the method body of doStuff() (making it do nothing) and you would still have to handle the exception in someMethod().
Upvotes: 2
Reputation: 4263
You did indeed catch MyException, but you re-threw it, so a new, active exception needs to be caught.
This is called a checked exception. Anytime you call the doStuff() method, you need to wrap it in a try/catch for MyException OR you can declare that your method will also throw MyException.
This guarantees that known exceptions will be at least thought about in the coding process.
Upvotes: 7