Nick Spacek
Nick Spacek

Reputation: 4773

Cannot cast from Throwable to MyException

I am catching an Exception and trying to examine the getCause() of it, performing some further actions if the cause is a of type MyException, defined in another library.

I am getting this Eclipse (compiler?) error when trying to check if e.getCause() instanceof MyException:

Incompatible conditional operand types Throwable and MyException

When attempting to cast (MyException) e.getCause(), I get:

Cannot cast from Throwable to MyException

I can compiled e.getCause().getClass().equals(MyException.class), and this does return true.

Upvotes: 4

Views: 4462

Answers (1)

Nick Spacek
Nick Spacek

Reputation: 4773

The solution is that MyException inherited from an Exception in an external library, but the top-level project didn't include that external library. I discovered this when I created a private class that extended MyException, and got an inconsistent type hierarchy error.

Upvotes: 3

Related Questions