Throwable getCause returns null
Asked Answered
P

2

6

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){
}
Pelargonium answered 7/4, 2014 at 6:1 Comment(5)
1. you should NOT catch Throwable; 2. .getCause() tries and sees what the parent Throwable is, if any.Iniquitous
Why do you expect the cause to be non-null here? Not every exception is caused by another one...Pumice
@Jon Skeet, but why eclspse debugger sees the cause?Pelargonium
@yuris: The t variable is the NoClassDefFoundError. Just call System.out.println(t) or System.out.println(t.getMessage()). If you expand t in Eclipse, you'll find that that exception doesn't have a cause... I think you've misunderstood what getCause() is there for.Pumice
@Pelargonium probably because Eclipse wraps the exception into one of its own; but on actual runs you won't have EclipseIniquitous
G
5

The answer is in the docs - Throwable#getCause:

Returns the cause of this throwable or null if 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);
}
Gonsalve answered 7/4, 2014 at 6:6 Comment(0)
V
0

The cause value is the cause of this throwable exception. To make getCause() return a value you can check an example of my peace of code.

class Dog {

    public void makeSound() {
        System.out.println("Bark Bark");
        throw new NullPointerException("message");
    }
}
    try {
            // create an object of Dog
            Dog dog = new Dog();

            // create an object of Class
            // using getClass()
            Class obj = dog.getClass();

            // using object of Class to
            // get all the declared methods of Dog
            Method[] methods = obj.getDeclaredMethods();

            // create an object of the Method class
            for (Method m : methods) {
                Object[] parameters = new Object[] {};
                m.invoke(dog, parameters);
            }
        } catch (InvocationTargetException e) {
            System.out.println(e); 
        } catch (Exception ex) {
            System.out.println(ex);
        }

FYI: e.getTargetException() is the same as e.getCause()

InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor.

InvocationTargetException DOC link

Some related info Chained Exceptions

Vocalist answered 12/8, 2022 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.