Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>How do I throw exception to the outside of the thread?</p> </blockquote> <p>Couple ways you can do this. You can set a <code>UncaughtExceptionHandler</code> on the thread or you can use an <code>ExecutorService.submit(Callable)</code> and use the exception that you get from the <code>Future.get()</code>.</p> <p>The easiest way is to use the <code>ExecutorService</code>:</p> <pre><code>ExecutorService threadPool = Executors.newSingleThreadScheduledExecutor(); Future&lt;Void&gt; future = threadPool.submit(new Callable&lt;Void&gt;() { public Void call() throws Exception { // can throw OhNoException here return null; } }); // you need to shut down the pool after submitting the last task threadPool.shutdown(); // this can throw ExecutionException try { // this waits for your background task to finish, it throws if the task threw future.get(); } catch (ExecutionException e) { // this is the exception thrown by the call() which could be a OhNoException Throwable cause = e.getCause(); if (cause instanceof OhNoException) { throw (OhNoException)cause; } else if (cause instanceof RuntimeException) { throw (RuntimeException)cause; } } </code></pre> <p>If you want to use the <code>UncaughtExceptionHandler</code> then you can do something like:</p> <pre><code> Thread thread = new Thread(...); final AtomicReference throwableReference = new AtomicReference&lt;Throwable&gt;(); thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() { public void uncaughtException(Thread t, Throwable e) { throwableReference.set(e); } }); thread.start(); thread.join(); Throwable throwable = throwableReference.get(); if (throwable != null) { if (throwable instanceof OhNoException) { throw (OhNoException)throwable; } else if (throwable instanceof RuntimeException) { throw (RuntimeException)throwable; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload