RethrowExecughtExceptionHandler异常记录后 [英] Rethrow UncaughtExceptionHandler Exception after Logging It

查看:168
本文介绍了RethrowExecughtExceptionHandler异常记录后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序类中,我试图在发生之前抓住一个力,所以我可以记录它,然后重新抛出它,所以android可以处理它。我这样做是因为有些用户没有报告强制关闭。



我正在eclipse开发,eclipse不允许我重新抛出异常。它显示错误说未处理的异常类型Throwable:环绕与try / catch。如何重新启动异常?

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

尝试
{
//在应用程序强制关闭之前记录异常
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。请报告Force Close,以便我可以修复它,Toast.LENGTH_LONG);

//重新抛出异常所以用户可以报告它
// throw ex; //&l t; - ** eclipse显示一个错误环绕与try / catch **
}
});

} catch(异常e)
{
e.printStackTrace();
}
}

}

解决方案

抱歉,不是Android专家 - 但是看起来你不能抛出ex,因为你的方法签名void uncaughtException(Thread,Throwable)不声明它抛出任何东西。



假设你覆盖了一个API接口,(a)不能修改这个签名,(b)不想因为你会抛出的上下文,你可以改为使用装饰器模式,并且基本上将子类化为默认的UncaughtExceptionHandler实现来记录您的消息,然后按照惯例进行处理?



编辑:未测试,但这可能看起来有点像:

  final UncaughtExceptionHandler subclass = Thread.currentThread()。getUncaughtExceptionHandler(); 
Thread.currentThread()。setUncaughtExceptionHandler(new UncaughtExceptionHandler(){
@Override
public void uncaughtException(Thread thread,Throwable ex){
// your code
分析用例(MainApplication.this).trackEvent(
Errors,//类别
MainActivity,// Action
强制关闭:+ ex.toString(),/ / Label
0); // Value
AnalyticsUtils.getInstance(MainApplication.this).dispatch();
Toast.makeText(MainApplication.this,Snap!Something broke。请报告强制关闭,所以我可以修复它。,Toast.LENGTH_LONG).show();

//继承以前的流
subclass.uncaughtException(thread,ex);
}
});


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.

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();
    }
}

}

解决方案

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.

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?

Edit: 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).show();

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

这篇关于RethrowExecughtExceptionHandler异常记录后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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