Suppose I catch an exception that is of type AppException
but I only want to carry out certain actions on that exception if it has a nested exception of type StreamException
.
if (e instanceof AppException)
{
// only handle exception if it contains a
// nested exception of type 'StreamException'
How do I check for a nested StreamException
?
Exception handling begins again with the most recently generated exception. Note: If a nested exception causes the program to end, the exception handler for the first exception may not complete. In this example, the main() function generates an exception which causes main_hdlr to get control.
Java Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc. Exception is an unwanted or unexpected event, which occurs during the execution of a program, i.e. at run time, that disrupts the normal flow of the program's instructions.
The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that the normal flow of the application can be maintained. In this tutorial, we will learn about Java exceptions, it's types, and the difference between checked and unchecked exceptions.
The getCause() method is from Throwable class and we can use this method which returns the cause of the exception or returns null if the cause of the exception is not known.
Do: if (e instanceof AppException and e.getCause() instanceof StreamException)
.
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