How Will You Handle InterruptedException in Java ?

In Java we can get InterruptedException from sleep() or join() methods. Throwing InterruptedException is way to inform that another thread has interrupted this thread.

In general, the purpose of Interrupt is to ask current thread to stop its current execution and finish unexpectedly.

Therefore ignoring this exception by catching it and only logging it to the console or some log file is not the recommended approach.

The run() method of the Runnable interface does not allow that throwing any exceptions. So we cannot re throw InterruptedException.

Therefore the correct way to handle this exception is that run() method should check and handle this exception by itself and take appropriate action.



You May Interest

What is the Reason to Organize Classes and Interfaces in a Packag ...

What is the Difference Between a Set and a Map in Java ?

What are the Differences Between Collection and Collections in Ja ...

Creating a Map with Reverse View and Lookup in Java

What is the Difference Between Expression and Scriptlet in JSP ?