问题
I often see catch clauses for Throwable in Blackberry documentation, such as the Network API docs. My sense is that this is not generally a good practice in Java.
Is there a reason for this in Blackberry programming?
Does it have to do with stack trace generation for Throwables?
回答1:
When you catch Throwable in a BlackBerry app, not only does it preserve the stack trace, it saves that stack trace in the device event log. There is no way for an app to get a stack trace itself, so unfortunately you can't automatically collect stack traces.
To view the stack trace, you pull up the event log viewer. For blackberries with physical keyboards, hold 'Alt' and then press L G L G to bring up the event log.
回答2:
Read the documentation for java.lang.Error, which is a subclass of Throwable, and you'll see the problem with catching Throwable.
It says:
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.
For example, you could end up inadvertently catching a VirtualMachineError indicating the whole VM is in a broken state. Putting something in the finally block to run on broken VM doesn't seem like a good idea.
回答3:
I don't think there's a special reason. See the comment:
} catch (Throwable t) { // can also catch specific exceptions to include special hadling for the different types
It means the example is basic. And has a typo, and a bad practice. So catch specific exceptions if possible.
回答4:
On the BB platform, if Throwable is caught it preserves the stacktrace and usually renders it on the screen, blasting in the user's face. Not very good for UX :(
When Exception (and extended classes) are caught the stacktrace is thrown away for efficiency reasons.
来源:https://stackoverflow.com/questions/5267160/catching-throwable-in-blackberry-java-good-idea