public class TestInfo {
    private static String str =null;
    public static void main(String[] args) {
        System.out.println("test exception");
        try {
            if(str.equals("name")){
                System.out.println("test exception");
            }
        } catch (Exception e) {
            System.out.println(e.toString());
            System.out.println(e.getMessage());
        }
    }
}
输出结果:
; font-family:" microsoft="" background-color:="" /> null

示例代码2:
public class TestInfo {
    private static int m = 0;
    public static void main(String[] args) {
        System.out.println("test exception");
        try {
            m = 899/0;
        } catch (Exception e) {
            System.out.println(e.toString());
            System.out.println(e.getMessage());
        }
    }
}
输出结果:
java.lang.ArithmeticException: / by zero
/ by zero


总结:
由此可以看出,e.toString()获取的信息包括异常类型和异常详细消息,而e.getMessage()只是获取了异常的详细消息字符串。

相关文章:

  • 2021-09-23
  • 2022-12-23
  • 2021-09-30
  • 2021-11-16
  • 2021-07-01
  • 2021-11-16
猜你喜欢
  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
  • 2021-07-02
  • 2021-12-15
  • 2022-01-08
  • 2021-12-05
相关资源
相似解决方案