java - Can we catch an exception without catch block? -
suppose have try
-finally
block without catch block, throw exception inside try
block. able catch exception?
public static void main(string[] args) throws ioexception{ try { throw new ioexception("something went wrong"); } finally{ } }
yes, possible.
you can use uncaught exception handler. responsibility catch exceptions program didn't catch, , it.
public static void main(string[] args) throws ioexception { thread.setdefaultuncaughtexceptionhandler((thread, thr) -> thr.printstacktrace()); throw new ioexception("something went wrong"); }
setdefaultuncaughtexceptionhandler
method register handler invoked when exception has been thrown in thread , wasn't caught. above code print stacktrace of throwable handled.
the handler takes argument thread exception happened , throwable thrown.
you can have handler per thread using setuncaughtexceptionhandler
on thread
instance. handler handle uncaught exceptions thrown thread.
Comments
Post a Comment