本文介绍了登录后重新抛出的UncaughtExceptionHandler异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序类,我想抓住一个强制关闭它发生之前,这样我就可以登录,然后重新抛出这样的机器人可以处理它。我这样做是因为有些用户不报的力量关闭。

In my Application class I am trying to catch a force close before it happens, so I can log it and then rethrow it so the android can handle it. I do this since some users do not report force closes.

我开发的日食,月食,并没有让我重新抛出异常。它显示了一个错误说未处理的异常Throwable类型:环绕的try / catch。我怎样才能重新抛出异常?

I am developing in eclipse, and eclipse is not allowing me to rethrow the exception. It shows an error saying "Unhandled exception type Throwable: Surround with try/catch". How can I rethrow the exception?

public class MainApplication extends Application
{
    @Override
    public void onCreate()
    {
    super.onCreate();

    try
    {
        //Log exception before app force closes
        Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable ex) {
                AnalyticsUtils.getInstance(MainApplication.this).trackEvent(
                        "Errors",                       // Category
                        "MainActivity",                 // Action
                        "Force Close: "+ex.toString(),  // Label
                        0);                             // Value
                AnalyticsUtils.getInstance(MainApplication.this).dispatch();

                Toast.makeText(MainApplication.this, "Snap! Something broke. Please report the Force Close so I can fix it.", Toast.LENGTH_LONG);

                //rethrow the Exception so user can report it
                //throw ex; //<-- **eclipse is showing an error to surround with try/catch**
            }
        });

    } catch (Exception e)
    {
        e.printStackTrace();
    }
}

}

推荐答案

道歉,而不是Android的专家 - 但看起来像你不能把恩,因为你的方法签名无效uncaughtException(螺纹,可抛出)没有声明它抛出什么。

Apologies, not an Android expert - but looks like you can't throw ex because your method signature "void uncaughtException(Thread, Throwable)" doesn't declare that it "throws" anything.

假设你覆盖的API接口和(a)不能修改这个签名和(b)不想,因为你会扔它断章取义,你能改为使用Decorator模式,基本上子类的默认UncaughtExceptionHandler的实施来记录你的邮件,然后让它在处理执行像往常一样?

Assuming you're overriding an API interface and (a) can't modify this signature and (b) don't want to because you'd be throwing it out of context, could you instead use a decorator pattern and basically subclass the default UncaughtExceptionHandler implementation to log your message and then let it carry on processing as usual?

编辑:未经检验的,但是这看起来有点像:

untested, but this might look a bit like:

    final UncaughtExceptionHandler subclass = Thread.currentThread().getUncaughtExceptionHandler();
    Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            // your code
            AnalyticsUtils.getInstance(MainApplication.this).trackEvent(
                    "Errors",                       // Category
                    "MainActivity",                 // Action
                    "Force Close: "+ex.toString(),  // Label
                    0);                             // Value
            AnalyticsUtils.getInstance(MainApplication.this).dispatch();
            Toast.makeText(MainApplication.this, "Snap! Something broke. Please report the Force Close so I can fix it.", Toast.LENGTH_LONG);

            // carry on with prior flow
            subclass.uncaughtException(thread, ex);
        }
    });

这篇关于登录后重新抛出的UncaughtExceptionHandler异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 16:51