I want to throw an exception and to display it. In my IF block I throw the exception. Do I have to store the exception to pass it as a variable in the method which precedes.
if(count !=0) {
throw new Exception();
eventLogger.logError("The count is not zero",e)
}
else{
// do something else
}
The logger has Throwable error as a parameter.
logError(String description, Throwable error);
How can I pass the exception I thrown to this method
Once the Exception is thrown, your program stops running. Thus you have to change the order of your statements.
if(count !=0) {
Exception e = new Exception();
eventLogger.logError("The count is not zero",e)
throw e;
}
else{
// do something else
}
As you can read in the Java API the class Exception extends Throwable
EDIT
As Zabuza mentioned, it's possible to handle the exception in a try- catch block. This would be a more elegant way:
try{
if(count !=0) {
throw new Exception();
}else{
// do something else
}
}
catch(Exception e){
eventLogger.logError("The count is not zero",e)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With