java - Why must I use try catch when I use global exception handling? -
i'm beginner in java , android. problem when use setdefaultuncaughtexceptionhandler in code, functions still need try/catch block surrounding it, want throw exceptions uncaughtexception thread.
public class myalarmreciever extends broadcastreceiver { @override public void onreceive(context context, intent intent) { thread.setdefaultuncaughtexceptionhandler(new uncaughtexception(context)); try { string imageurl = mywebservice.readfeed(); downloadandset.downloadfile(imageurl); } catch(throwable e) { throw new runtimeexception(e); } toast.maketext(context, "alarm triggered", toast.length_long).show(); } }
java distinguishes checked , unchecked exceptions. checked exceptions have to caught, no matter what.
correction: or have add throws clause method. postpones urge catch exception caller of method.
if want them handled in uncaughtexceptionhandler, can "forward" them:
try{ // blah "throws checked exception type" } catch ( throwable e ) { // throw e; <- not work :( unless add "throws" clause. throw new runtimeexception(e); } unfortunately, throwing same exception won't work, because you'd have add throws clause, not want. you'll have wrap in runtimeexception.
Comments
Post a Comment