Home > Java > javaTutorial > body text

The differences between egetMessage() and toString() methods in Java

巴扎黑
Release: 2017-07-20 13:15:47
Original
1845 people have browsed 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


Summary:
It can be seen that the information obtained by e.toString() includes the exception type and exception detailed message, while e.getMessage() only obtains the detailed message string of the exception.
#

The above is the detailed content of The differences between egetMessage() and toString() methods in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template