简体   繁体   English

当我无法使用调试器时,如何以字符串形式获取错误消息

[英]How can I get an error message as a string when I can't use the debugger

I want to be able to save an entire error log to a String in java.我希望能够将整个错误日志保存到 java 中的字符串中。 For example, if I have the line例如,如果我有这条线

int a = 0/0

then this gives the following error message in a debugger:那么这会在调试器中给出以下错误消息:

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at HelloWorld.main(HelloWorld.java:8)

What I want is to be able to have this above error message entirely in a string.我想要的是能够将上述错误消息完全放在一个字符串中。 I've tried surrounding the code I want the error string for (for this example, the division by zero) in a try/catch block as follows, but this only gives me the java.lang.ArithmeticException: / by zero portion of it.我已经尝试在 try/catch 块中包围我想要错误字符串的代码(对于这个例子,除以零),但这只会给我java.lang.ArithmeticException: / by zero部分.

This is my try/catch block:这是我的 try/catch 块:

try{
       int a = 0/0;
   }catch(Exception e){
       String a = getFullErrorString(e);
   }

With my method being:我的方法是:

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

I need to do this because my app does not work with a debugger and an external device at the same time (the external device uses the USB-C port so I can't debug with it plugged in, and Bluetooth is not an option because my app makes heavy use of Bluetooth).我需要这样做,因为我的应用程序不能同时使用调试器和外部设备(外部设备使用 USB-C 端口,因此我无法在插入它的情况下进行调试,并且蓝牙不是一个选项,因为我的应用大量使用蓝牙)。 Please help!请帮忙!

Here is a class I find useful for catching and logging exceptions:这是一个 class 我发现对捕获和记录异常很有用:

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

The stack trace is written to a file.堆栈跟踪被写入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得错误消息? - How can I get error message? 为什么我无法使Google Cloud Debugger正常工作? - Why can't I get the Google Cloud Debugger to work? 如果数字超出范围,如果语句提示用户出现错误消息,我如何获取Java - how can i get java if statment promopt a user with error message when a number out of the range 如何使用oozie获得更精确的错误消息? - How can i get more precise error message with oozie? 如何从后端路由器获取错误消息? - How can i get error message from backend router? 如何修复“附加调试器或 ESC 以取消”错误? - How can I fix "Attach a Debugger or ESC to Cancel" error? 在消息驱动bean上收到JMS消息时,如何通知Cometd服务器实例? - How can I notify the cometd server instance when I get JMS message on Message driven bean? 我如何才能将最终字符串转换为功能以用于其他功能? - How can i get final string into function for use to another function? 为什么我在使用 ItemSpatialMark 时不能使用 setUnlocalizedName(String) return undefined - Why can't I use setUnlocalizedName(String) return undefined when I use ItemSpatialMark 我如何在 Java 中显示错误消息 - How can i display an error message in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM