I am catching a Throwable error.
catch (Throwable t){
System.out.println(t.getCause().getMessage());
}
When I put a breakpoint on a line System.out., and hover over (Eclipse) the t variable, Eclipse shows me the cause: java.lang.NoClassDefFoundError: myclass
But when I release the breakpoint I am getting null for t.getCause().
Why does it happen? How can I get the cause string.
UPDATE:
Same happens if I catch
catch (NoClassDefFoundError e){
}
The answer is in the docs - Throwable#getCause:
Returns the cause of this throwable or
nullif the cause is nonexistent or unknown. (The cause is the throwable that caused this throwable to get thrown.)
So when you do:
t.getCause().getMessage()
It's like writing:
null.getMessage()
Which of course causes NullPointerException.
You can simply do:
catch (NoClassDefFoundError e){
System.out.println(e.getMessage);
}