捕获所有类型的异常编程机器人 [英] Catch all type exceptions programming Android

查看:178
本文介绍了捕获所有类型的异常编程机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发Android操作系统的应用程序。 由于这是我的第一个应用程序,我想我已经犯了一些编程错误,因为我几乎没有能够追踪Bug,回到他们的原因。 因此,我猜测,而我试图修正错误,有没有办法来捕获所有类型的异常在我的整个生命周期中的活动与一个的try-catch?

I'm developing an application for Android OS. Since this is my first application, I think I've committed some programming mistakes cause I hardly can trace bugs back to their causes. Thus, I was guessing, while i'm trying to fix bugs, is there a way to catch ALL types of exception in my entire activity lifecycle with one try-catch?

这将是真棒,我越来越无聊的看着我的Galaxy S说:对不起应用程序应用程序已经停止,意想不到:(

That would be awesome, i'm getting bored watching my galaxy S say :"Sorry the application App has stopped unexpectly" :(

推荐答案

我真的,真的不建议这样做......

I really, really don't recommend this...

try {
     ...
} catch (Exception e) {
     // This will catch any exception, because they are all descended from Exception
}

您看您的堆栈跟踪调试你的问题是什么?它不应该是很难跟踪他们失望。看看LogCat中和审查的红色文字的大块,看看哪些方法会造成你的崩溃和你的错误了。

Are you looking at your stack traces to debug your issues? It should not be hard to track them down. Look at LogCat and review the big block of red text to see which method caused your crash and what your error was.

如果你发现你所有的错误,这样一来,你的程序是不会像预期的那样,你不会得到从Android Market的错误报告,当你的用户报告他们。

If you catch all your errors this way, your program is not going to behave as expected, and you will not get error reports from Android Market when your users report them.

您可以使用的UncaughtExceptionHandler来可能prevent有些崩溃。我用一个,但只打印堆栈跟踪到一个文件中,当我在手机上调试一个应用程序从我的电脑。但我通过对未捕获的异常为默认的Andr​​oid的UncaughtExceptionHandler我做到了这一点之后,因为我要的Andr​​oid要能够正确地处理它,并给用户送我一个堆栈跟踪的机会。

You can use an UncaughtExceptionHandler to possibly prevent some crashes. I use one, but only to print stack traces to a file, for when I'm debugging an app on a phone away from my computer. But I pass on the uncaught exception to the default Android UncaughtExceptionHandler after I've done that, because I want Android to be able to handle it correctly, and give the user the opportunity to send me a stack trace.

这篇关于捕获所有类型的异常编程机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆