I was wondering how to handle a thrown exception that is thrown by the Thread.sleep method and the object class wait method
There are two important things to InterruptedException
. First of all, when it is reset, the interrupt flag on Thread
is cleared. Therefore, you should always do something like:
} catch (InterruptedException e) {
This is a very important pattern, because it allows another code that may call yours to also detect an interrupt.
Secondly, in terms of flows, if they are interrupted, they should most likely clear and exit. This, of course, is up to you, the programmer, but the general behavior is to shut down and exit the operation being performed - often because the program is trying to shut down.
} catch (InterruptedException e) { Thread.currentThread().interrupt();
Finally, with any exception handling, the default Eclipse template is usually incorrect. Never just e.printStackTrace();
an exception. Find out what you want to do with it: repeat the throw as another exception, write it somewhere better, or exit the thread / method / application.
source share