Vitali Melamud
Vitali Melamud

Reputation: 1397

Java thread crashes silently - I can't catch the exception

I see a behavior from I can't explain

I have a Runnable that it's code is something of:

run()
{
    print("start");
    try{
        doSomething();
        print("end");    

    }catch (Exception e){
        print("problem");
    }
    print("method end");

}

The behavior is strange because the only print I get is "start" - I would expect to get the "problem" and the "method end" prints also. And if the doSomething handles the exception itself - so the "end" would be printed. But I don't get any of the prints except "start"

Now i know that there is some issue with the doSomething method as if I run it not through the executor service, I get "Method Not Found" exception.

I think it might be somehow connected to the Executor service that I use - Executors.newFixedThreadPool, but I can't explain it

Thanks for your advice!

Upvotes: 2

Views: 3244

Answers (2)

hmjd
hmjd

Reputation: 121961

NoSuchMethodError is not derived from Exception:

java.lang.Object
    java.lang.Throwable
        java.lang.Error
            java.lang.LinkageError
                java.lang.IncompatibleClassChangeError
                    java.lang.NoSuchMethodError

so will not be caught by the posted catch clause. Change to catch Throwable or add a separate catch clause for Error and handle differently (attempt to recover from an Exception but report an Error and do not attempt to recover for example).

Upvotes: 11

cadrian
cadrian

Reputation: 7376

Certainly an Error is thrown. Try catching Throwable instead.

Edit: Indeed, java.lang.NoSuchMethodError is an Error.

Upvotes: 4

Related Questions