1

I've code that looks like this:

public static void startService() { try{ new Thread(new Runnable() { @Override public void run() { throw new RuntimeException("Some exception"); } }).start(); }catch (Exception e){ //Exception handling } } 

How can I handle this exception in the catch() block in parrent thread? UPD: This threads have to work asynchronous

2
  • Take a look at stackoverflow.com/questions/2631791/… Commented Jul 4, 2013 at 10:08
  • One has to realize that new Thread() and .start() are not likely to throw an exception. Then a parallel thread is startend and run called. Via an exception handler you can be informed. Commented Jul 4, 2013 at 10:21

5 Answers 5

2

You have several options to handle exceptions thrown by threads. One is to have a general uncaught exceptions handler:

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { //log the exception or handle it here if possible } }); 

But it is difficult to link an exception caught that way with a specific thread.

Or you can use an ExecutorService instead of starting the thread manually:

ExecutorService executor = Executors.newCachedThreadPool(); Future<?> future = executor.submit(new Runnable() { @Override public void run() { throw new RuntimeException("Some exception"); } }); try { future.get(); } catch (ExecutionException e) { Throwable yourException = e.getCause(); //here you can access the exception } 
Sign up to request clarification or add additional context in comments.

2 Comments

I'm sorry, I didnt mention that i need to do it asynchronous. And method get() is synchronous.
It is fine, you can have the get() method run in its own thread and then register a listener
0

If you mean it is inside of Runnable's run() method then you will have to use another approach. Use Callable instead! Callable call() method allows you to return a value and throw an exception.

Please have a look here for an example on how to use Callable. Also, note that it is better to use a higher level api such as ExecutorService which manages the lifecycle of your threads and provides thread pooling. (included in the example)

Comments

0

you would have to use an Callable.

The thread.run method will never throw an exception since it is - well - executed in a different thread and in this was will not interfere with your calling thread.

if you execute a callable (by e.g. running it via an ExecutorService) you get a Future result which in turn will throw the given exception when calling the future.get() method.

Comments

-1

Use throw statement in catch block.

public static void startService() { try{ new Thread(new Runnable() { @Override public void run() { } }).start(); }catch (Exception e){ throw e; } } 

1 Comment

I think the question is: how can I rethrow the exception thrown by the child thread from the parent thread.
-2

Rethrowing an exception is simply adding throw e in the catch block

6 Comments

downvote means I made some mistake in explaining about thread, can you please let me know ?
upvoted to restore the damage, other people have also received downvotes without explanation
You can't catch the exception thrown in the Runnable run's method from the main thread with the method you describe.
@assylias, thank you :) thats a new thing to learn. Edited my answer for the same
@PrasadKharkar Your answer still does not answer the question asked I'm afraid. What you say is true in general but not applicable to the specific situation described in this post.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.