【问题标题】:Crash An App using Exception in Android在 Android 中使用异常使应用程序崩溃
【发布时间】:2012-12-09 15:28:01
【问题描述】:

我只想让 Android 应用程序崩溃并在 Android 中使用 Exception 显示“死亡对话框”(强制关闭对话框)。可能吗?它是如何完成的?

【问题讨论】:

  • int i = 1/0放在某处。

标签: java android exception crash


【解决方案1】:

您想要的是RuntimeException,否则编译器会强制您将代码移动到 try-catch 块中。

throw new RuntimeException("Crash!");

【讨论】:

  • 终于到了,我可以在任何函数中执行throw new RuntimeException(new MyException()),而无需放置throws Exception 和try-catch 块。谢谢!
【解决方案2】:

我认为您正在寻找 uncaughtExceptionHandler。

这是一个很好的例子,

http://iamvijayakumar.blogspot.in/2012/09/android-uncaughtexceptionhandler.html

在您的 Activity 中使用此代码来注册未捕获的异常处理程序,

Thread.setDefaultUncaughtExceptionHandler(new UnCaughtException(MainActivity.this));

【讨论】:

  • 我可以结合@Tom G 的回答和你的回答,谢谢。
【解决方案3】:

尝试将任何错误的语句放在 try 块中,有很多异常可用,例如 arithmeticarrayindexoutofbounds 等等,然后在 catch 中做你想做的事情。

并且为了显示强制关闭对话框,默认情况下会出现强制关闭对话框,您无需执行任何操作。

为什么要在应用崩溃时捕获异常?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多