BlackBerry - global exception handler

杀马特。学长 韩版系。学妹 提交于 2019-12-22 00:29:50

问题


(edit: this question is about BB specifically, because of the strange way it optimises exceptions. I am comfortable with normal exception handling patterns in J2SE, but BB does not behave as per normal. Specifically in this case, BB discards the error type, and message, and how BB devs try to deal with this issue, or if they ignore it.)

I would like to implement some form of custom global error handling in my BB app. Specifically to try to handle any other exceptions that were not caught by my code, because I had not expected them. The default behaviour is that the app fails, and a dialog pops up saying an Unknown error occured.

I would like to describe the error a little bit better, hence my term "global error handler". Something similar to the code:

public static void main(String[] args)
{
    try
    {

        FusionApp app = FusionApp.getInstance();
        app.enterEventDispatcher();

    }
    catch (Throwable t)
    {
        // t has lost all type information at this point - this prints "null"
        System.err.println(t.getMessage());
    }
}

My immediate problem is that when I catch t (in the main() method after the app.enterEventDispatcher() call), it has lost its type information. e.g. I know that the code throws an IllegalArgumentException with a custom message - however in the catch block, it is a java.lang.Error with null message.

And in the stack trace (ALT LGLG), the message has also been lost (at least the stack trace is accurate).

So... what is a good pattern to use to implement some form of global error handling on BB? Or is this considered a bad idea on this platform?

Is it considered good practice to just have the unknown error dialog box pop up - I don't like this, but maybe that is the way of the BB?


回答1:


Best practices are to implement custom exception handling.

So, if you expecting to catch IllegalArgumentException, MyCustomException and StartupException, put them into catch block first, and then put an Exception catch (and then, if you like, put a Throwable catch)

The common rule is - from most exclusive to most common, and for exceptions of the same level - from most expected to least expected.

In case of exception == null or getMessage() == null you can always display something like "Application error, please send event log to [support email]" message, then if you have a nice event logging in you app, you have a good chance to reproduce an issue.

And talking about event log, see EventLogger class to implement logging.



来源:https://stackoverflow.com/questions/6096705/blackberry-global-exception-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!