-1

I am having a problem understanding the reason why for Thread.sleep(), the InterruptedException error is caught by the JVM and for ThreadExecp.fun() that is not the case.

public class ThreadExecp {
static void fun() throws InterruptedException
{
    System.out.println("Inside fun(). ");
    throw new InterruptedException ();
}

public static void main(String args[]) throws InterruptedException {

    Thread.sleep(10000);
    System.out.println("done 1");
    ThreadExecp.fun();
    System.out.println("Done");

}
}

Where are actually the errors that occur inside the main method caught and why some are that can simply be thrown and some need to be caught inside the try-catch?

  • 1
    Any exception can be thrown if your method declares that it `throws` it. Typically the ones that are thrown out of `main` will kill your program. – khelwood Aug 02 '20 at 20:05
  • Exceptions are generally the result of things that could leave your code in an inconsistent state. Therefore if the programmer doesn't handle them via throw/catch, then it was deemed best to end the program with a crash before it corrupts your files. – NomadMaker Aug 02 '20 at 20:31

2 Answers2

1

InterruptedException is not caught anywhere here, it's simply propogated along the call stack. And there's nothing in your code ready to catch and handle it, the JVM has no other choice but to take care of the unhandled exception.

nylanderDev
  • 531
  • 4
  • 14
1

Throws for main function doesn't catch error

No 'throws' or 'throw' catches an exception/error. Only 'catch' catches an exception/error.

The point of a 'throws' clause on a method declaration is to state that this method may throw the stated exceptions.

The caller of such a method then has two choices for its coding: (1) catch the stated exception, or (2) declare through a 'throws' that it may pass on that exception.

And so on, back up the call chain.

The buck has to stop with 'main'. Either 'main' has an exception handler (try-catch statement) for the exception, or else it passes it along ('throws') to its caller, which loosely speaking is the JVM itself.

In your case there are two distinct reasons why your 'main' might see an InterruptedException - the call to fun() and the call to sleep(). Both of these are declared as potentially throwing that exception.

The statement of your question shows a little confusion. I think perhaps you're misunderstanding the way in which exceptions are propagated back up the call stack until a handler is located. And in your example main, there is no handler at all.

Personally, I think it's poor style to put a 'throws' on main. It means that your program knows a certain exception is possible and is going to leave it to the JVM to do something about it. You should rather write an exception handler that explicitly does whatever you consider appropriate. In your example case, the exception handler might just print "interrupted", for example - that seems to fit with the rest of the code.

user13784117
  • 1,124
  • 4
  • 4