java - Who and how handle the exception when we use throws? -
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 handling ioexception here ? whe use try-catch can handle in catch block. here how , handling ?
when have method throws
clause, other method calls method has either handle exception (by catching it) or throwing furter having throws
clause type of exception (so that, in turn, method calls 1 again has same, etc.).
when main
method has throws
clause, jvm take care of catching exception, , default print stack trace of exception.
when want special handling when main
throws exception, can set uncaught exception handler:
thread.setdefaultuncaughtexceptionhandler(new uncaughtexceptionhandler() { @override public void uncaughtexception(thread t, throwable e) { system.err.printf("thread %s threw uncaught exception!%n", t.getname()); e.printstacktrace(); } });
Comments
Post a Comment