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){
}
Maroun
  • 94,125
  • 30
  • 188
  • 241
yuris
  • 1,109
  • 4
  • 19
  • 33
  • 1
    1. you should NOT catch `Throwable`; 2. `.getCause()` tries and sees what the _parent_ `Throwable` is, if any. – fge Apr 07 '14 at 06:02
  • 3
    Why do you *expect* the cause to be non-null here? Not every exception is caused by another one... – Jon Skeet Apr 07 '14 at 06:04
  • @Jon Skeet, but why eclspse debugger sees the cause? – yuris Apr 07 '14 at 06:05
  • 3
    @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. – Jon Skeet Apr 07 '14 at 06:06
  • @yuris probably because Eclipse wraps the exception into one of its own; but on actual runs you won't have Eclipse – fge Apr 07 '14 at 06:11

2 Answers2

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);
}
erip
  • 16,374
  • 11
  • 66
  • 121
Maroun
  • 94,125
  • 30
  • 188
  • 241
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

Roman Motovilov
  • 344
  • 3
  • 5