Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to throw a checked exception from a java thread?

Hey, I'm writing a network application, in which I read packets of some custom binary format. And I'm starting a background thread to wait for incoming data. The problem is, that the compiler doesn't let me to put any code throwing (checked) exceptions into run(). It says:

run() in (...).Listener cannot implement run() in java.lang.Runnable; overridden method does not throw java.io.IOException

I want the exception to kill the thread, and let it be caught somewhere in the parent thread. Is this possible to achieve or do I have to handle every exception inside the thread?

like image 746
mik01aj Avatar asked Sep 02 '09 17:09

mik01aj


People also ask

Can we throw checked exception in run method of thread?

In Java thread, the 'run' method cannot throw a 'checked exception'.

Can we throw exception from thread?

Can we throw exception from Run method of thread? You can throw exception but that is of no use. As it is ignored by jvm.

Can you throw a checked exception in Java?

Can throw checked and unchecked exceptions. We can declare both types of exceptions using throws clause i.e. checked and unchecked exceptions. But the method calling the given method must handle only checked exceptions. Handling of unchecked exceptions is optional.

Is there any way of throwing a checked exception?

Using the Throws keyword We can throw either checked or unchecked exceptions. The throws keyword allows the compiler to help you write code that handles this type of error, but it does not prevent the abnormal termination of the program.


2 Answers

To be able to send the exception to the parent thread, you can put your background thread in a Callable (it allows throwing also checked exceptions) which you then pass to the submit method of some Executor. The submit method will return a Future which you can then use to get the exception (its get method will throw an ExecutionException which contains the original exception).

like image 188
Esko Luontola Avatar answered Oct 07 '22 20:10

Esko Luontola


Caveat: this may not meet your needs if you have to use the exception mechanism.

If I understand you correctly, you don't actually need the exception to be checked (you've accepted the answer suggesting an unchecked exception) so would a simple listener pattern be more appropriate?

The listener could live in the parent thread, and when you've caught the checked exception in the child thread, you could simply notify the listener.

This means that you have a way of exposing that this will happen (through public methods), and will be able to pass more information than an exception will allow. But it does mean there will be a coupling (albeit a loose one) between the parent and the child thread. It would depend in your specific situation whether this would have a benefit over wrapping the checked exception with an unchecked one.

Here's a simple example (some code borrowed from another answer):

public class ThingRunnable implements Runnable {     private SomeListenerType listener;     // assign listener somewhere      public void run() {         try {             while(iHaveMorePackets()) {                  doStuffWithPacket();             }         } catch(Exception e) {             listener.notifyThatDarnedExceptionHappened(...);         }     }  } 

The coupling comes from an object in the parent thread having to be of type SomeListenerType.

like image 45
Grundlefleck Avatar answered Oct 07 '22 20:10

Grundlefleck