Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 记录后重新显示UncaughtExceptionHandler异常_Android_Eclipse_Exception_Exception Handling - Fatal编程技术网

Android 记录后重新显示UncaughtExceptionHandler异常

Android 记录后重新显示UncaughtExceptionHandler异常,android,eclipse,exception,exception-handling,Android,Eclipse,Exception,Exception Handling,在我的应用程序类中,我试图在强制关闭之前捕获它,这样我就可以记录它,然后重新刷新它,这样android就可以处理它。我这样做是因为有些用户不报告强制关闭 我是在eclipse中开发的,eclipse不允许我重新抛出异常。它显示了一个错误,提示“未处理的异常类型Throwable:around with try/catch”。如何重新显示异常 public class MainApplication extends Application { @Override public vo

在我的应用程序类中,我试图在强制关闭之前捕获它,这样我就可以记录它,然后重新刷新它,这样android就可以处理它。我这样做是因为有些用户不报告强制关闭

我是在eclipse中开发的,eclipse不允许我重新抛出异常。它显示了一个错误,提示“未处理的异常类型Throwable:around with try/catch”。如何重新显示异常

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();
    }
}
public类main应用程序扩展应用程序
{
@凌驾
public void onCreate()
{
super.onCreate();
尝试
{
//应用程序强制关闭前的日志异常
Thread.currentThread().setUncaughtExceptionHandler(新的UncaughtExceptionHandler()){
@凌驾
public void uncaughtException(线程,可丢弃的ex){
AnalyticsUtils.getInstance(MainApplication.this).trackEvent(
“错误”,//类别
“MainActivity”,//操作
“强制关闭:”+ex.toString(),//标签
0);//值
AnalyticsUtils.getInstance(MainApplication.this.dispatch();
Toast.makeText(main application.this,“啪!有东西坏了。请报告关闭力,以便我可以修复它。”,Toast.LENGTH\u LONG);
//重新显示异常,以便用户可以报告它

//throw-ex;//抱歉,不是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 
            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);
        }
    });

我不确定这是否是您想要的,但是您可以添加一个
finally
子句,该子句将在成功的
try
或已处理的
catch
后运行:

try {
    //your code
} catch (Exception e) {
    //print stack trace
} finally {
    //Log
}

我认为下面的代码应该可以工作。使用新线程执行处理程序代码允许应用程序显示警报/土司并执行其他操作

此外,您可以尝试在退出
run()
之前调用
System.exit(0)
。这将重新启动最后一个活动

我在姜面包和果冻豆上都做了检查


对于现在才发现这个问题的人,我不确定上面的答案在发布时是否正确,但现在不正确。您现在想要的是创建一个实现
Thread.UncaughtExceptionHandler
的新类,并在构造函数中保存
Thread.getCurrentUncaughtExceptionHandler()
到一个字段,然后在
uncaughtException(Thread,Throwable)
方法的末尾调用它的
uncaughtException(Thread,Throwable)
。然后从实例化类的任何代码调用
Thread.setUncaughtExceptionHandler()

所以完整的代码看起来像:

//MainApplication.java
公共类MainApplication扩展了应用程序{
@凌驾
public void onCreate(){
super.onCreate();
CustomUncaughtExceptionHandler cueh=新CustomUncaughtExceptionHandler();
setDefaultUncaughtExceptionHandler(cueh);
}
}
//CustomUncaughtExceptionHandler.java
类CustomUncaughtExceptionHandler实现Thread.UncaughtExceptionHandler{
private Thread.UncaughtExceptionHandler oldHandler;
CustomUncaughtExceptionHandler(){
oldHandler=Thread.getDefaultUncaughtExceptionHandler();
}
@凌驾
public void uncaughtException(线程,可丢弃的ex){
AnalyticsUtils.getInstance(MainApplication.this).trackEvent(
“错误”,//类别
“MainActivity”,//操作
“强制关闭:”+ex.toString(),//标签
0);//值
AnalyticsUtils.getInstance(MainApplication.this.dispatch();
if(oldHandler!=null){
uncaughtException(线程,ex);
否则{
系统出口(1);
}
}
}

您还可以将原始默认未捕获异常处理程序存储在其他位置,以便稍后恢复(或添加
getOldHandler()
方法)如果出于任何原因,您决定不再运行代码。

嗯,我认为您是对的。我不熟悉装饰器模式,您可以展示一些代码来告诉我如何做到这一点吗?是的,这很有效!谢谢Capn Sparrow。(注:祝酒词不显示。)这里有点晚了,但由于我刚刚使用了Capn Sparrow的解决方案,我应该指出toast没有显示出来,因为在makeText调用的末尾有now.show()。这让我非常感兴趣。这不完全是我想要的,因为我想要的是“一网打尽”这将在强制关闭我的应用程序之前记录任何异常。我使用try/catch,但只是为了防止出现问题。
final UncaughtExceptionHandler subclass = Thread.currentThread().getUncaughtExceptionHandler();
    Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            new Thread() {
                @Override
                public void run() {
                    Looper.prepare();  
                    // 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);

                    Looper.loop();
                }
            }.start();
        }
    });