BlackBerry - 全局异常处理程序
(编辑:这个问题具体是关于 BB 的,因为它优化异常的方式很奇怪。我对 J2SE 中的正常异常处理模式很满意,但是 BB 的行为并不符合正常情况。具体来说,在这种情况下,BB 会丢弃错误类型,和消息,以及 BB 开发人员如何尝试处理这个问题,或者他们是否忽略它。)
我想在我的 BB 应用程序中实现某种形式的自定义全局错误处理。特别是尝试处理我的代码未捕获的任何其他异常,因为我没有预料到它们。默认行为是应用程序失败,并弹出一个对话框,提示发生未知错误。
我想更好地描述该错误,因此我使用“全局错误处理程序”一词。与代码类似的东西:
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());
}
}
我直接的问题是,当我捕获 t
时(在 app.enterEventDispatcher()
之后的 main()
方法中) call),它已经丢失了它的类型信息。例如,我知道代码会抛出带有自定义消息的 IllegalArgumentException
- 但是在 catch 块中,它是带有 null
的 java.lang.Error
> 消息。
并且在堆栈跟踪(ALT LGLG)中,消息也丢失了(至少堆栈跟踪是准确的)。
那么...在 BB 上实现某种形式的全局错误处理的好模式是什么?或者这在这个平台上被认为是一个坏主意?
只弹出未知错误对话框是否被认为是好的做法 - 我不喜欢这样,但也许这就是 BB 的方式?
(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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最佳实践是实现自定义异常处理。
因此,如果您希望捕获 IllegalArgumentException、MyCustomException 和 StartupException,请先将它们放入 catch 块中,然后放入 Exception catch(然后,如果您愿意,放入 Throwable catch)。
通用规则是 - 从最专有到最常见,以及同一级别的例外情况 - 从最预期到最不预期。
在异常 == null 或 getMessage() == null 的情况下,您始终可以显示类似“应用程序错误,请将事件日志发送到 [支持电子邮件]”的消息,然后如果您的应用程序中有一个不错的事件日志记录,那么您就可以重现问题的好机会。
关于事件日志,请参阅 EventLogger 类来实现日志记录。
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.