Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you check both the exception's type as well as they type of the nested exception?

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?

like image 421
Timothy Avatar asked Jun 25 '11 17:06

Timothy


People also ask

How do you handle nested exceptions?

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.

What are exceptions and how they are handled in Java?

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.

Which two can be achieved in a Java application if exception handling techniques are implemented?

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.

How do you find the cause of exception?

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.


1 Answers

Do: if (e instanceof AppException and e.getCause() instanceof StreamException).

like image 88
Marcelo Avatar answered Sep 20 '22 11:09

Marcelo