class Test{
public static void main(String[] cv) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
}
}
When we’re writing throws then who is actually handling the IOException here ? Whe we use try-catch we can handle it in the catch block. But here how and who is handling ?
When you have a method with a
throwsclause, then any other method that calls that method has to either handle the exception (by catching it) or throwing it furter by also having athrowsclause for that type of exception (so that, in turn, the method that calls that one again has to do the same, etc.).When the
mainmethod has athrowsclause, then the JVM will take care of catching the exception, and by default it will just print the stack trace of the exception.When you want to do special handling when
mainthrows an exception, then you can set an uncaught exception handler: