当前位置: 移动技术网 > IT编程>开发语言>Java > 浅谈Java异常的Exception e中的egetMessage()和toString()方法的区别

浅谈Java异常的Exception e中的egetMessage()和toString()方法的区别

2019年07月22日  | 移动技术网IT编程  | 我要评论

exception e中e的getmessage()和tostring()方法的区别:

示例代码1:

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()只是获取了异常的详细消息字符串。

以上这篇浅谈java异常的exception e中的egetmessage()和tostring()方法的区别就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网