Who is calling the Java Thread interrupt() method if I’m not?

The Thread interrupt mechanism is the preferred way to get a (cooperating) thread to respond a request to stop what it is doing. Any thread (including the thread itself I think) could call interrupt() on a Thread. In practice, the normal use-cases for interrupt() involve some kind of framework or manager telling some worker thread … Read more

Why is “throws Exception” necessary when calling a function?

In Java, as you may know, exceptions can be categorized into two: One that needs the throws clause or must be handled if you don’t specify one and another one that doesn’t. Now, see the following figure: In Java, you can throw anything that extends the Throwable class. However, you don’t need to specify a … Read more

try catch finally question

The finally block (nearly) always executes, whether or not there was an exception. I say nearly because there are a few cases where finally isn’t guaranteed to be called: If there is an infinite loop or deadlock in your code so that the execution remains inside the try or catch blocks then the finally block … Read more