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

Timothy picture Timothy · Jun 25, 2011 · Viewed 39.3k times · Source

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?

Answer

Marcelo picture Marcelo · Jun 25, 2011

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