Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.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
Java 如果可以';不要使用调试器_Java_Android_Error Handling_Runtime Error - Fatal编程技术网

Java 如果可以';不要使用调试器

Java 如果可以';不要使用调试器,java,android,error-handling,runtime-error,Java,Android,Error Handling,Runtime Error,我希望能够用java将整个错误日志保存为字符串。例如,如果我有这条线 int a = 0/0 然后,调试器中会出现以下错误消息: Exception in thread "main" java.lang.ArithmeticException: / by zero at HelloWorld.main(HelloWorld.java:8) 我想要的是能够将上面的错误消息完全放在一个字符串中。我已经尝试在try/catch块中围绕我想要的错误字符串(在本例中为零除)的代码,如下所示,但

我希望能够用java将整个错误日志保存为字符串。例如,如果我有这条线

int a = 0/0
然后,调试器中会出现以下错误消息:

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at HelloWorld.main(HelloWorld.java:8)
我想要的是能够将上面的错误消息完全放在一个字符串中。我已经尝试在try/catch块中围绕我想要的错误字符串(在本例中为零除)的代码,如下所示,但这只给出了它的
java.lang.arithmetricException:/by zero
部分

这是我的try/catch块:

try{
       int a = 0/0;
   }catch(Exception e){
       String a = getFullErrorString(e);
   }
我的方法是:

public static String getFullErrorString(Throwable error){        
    return(
            error.getCause() == null ? String.valueOf(error) : (error + "\n" + getFullErrorString(error.getCause()))
            );       
}

我之所以需要这样做,是因为我的应用程序无法与调试器和外部设备同时工作(外部设备使用USB-C端口,因此我无法在插入该端口的情况下进行调试,而蓝牙不是一个选项,因为我的应用程序大量使用蓝牙)。请帮忙

我发现有一个类对于捕获和记录异常非常有用:

        String rootDir = Environment.getExternalStorageDirectory().getPath();

    //----------------------------------------------------
    // Define inner class to handle exceptions
    class MyExceptionHandler implements Thread.UncaughtExceptionHandler {
        public void uncaughtException(Thread t, Throwable e){
           java.util.Date dt =  new java.util.Date();
           String fn = rootDir + "/YourFilenameHere_" + sdf.format(dt) + "_DBG.txt";
           try{ 
              PrintStream ps = new PrintStream( fn );
              e.printStackTrace(ps);
              ps.close();
              System.out.println("wrote trace to " + fn);
              e.printStackTrace(); // capture here also???
//              SaveStdOutput.stop(); // close here vs calling flush() in class 
           }catch(Exception x){
              x.printStackTrace();
           }
           lastUEH.uncaughtException(t, e); // call last one  Gives: "Unfortunately ... stopped" message
           return;    //???? what to do here
        }
    }


    Thread.UncaughtExceptionHandler lastUEH = null;

// Then in onCreate:
        lastUEH = Thread.getDefaultUncaughtExceptionHandler();  // save previous one
        Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler());

堆栈跟踪被写入文件。

对于远程错误调试,您可以使用Firebase Crashlytics将文本写入文件有用吗?@NormR我确实将其写入日志文件,但我在问题中没有提到这一点,因为一旦将其作为字符串写入文件,就很简单了