Differences between the Egetmessage () and ToString () methods in Java exception exception E

Source: Internet
Author: User
Tags getmessage

The difference between the GetMessage () and ToString () methods of E in Exception e:
Example code 1:
Public Classtestinfo {
Privatestatic String str =null;
publicstatic void Main (string[] args) {
System.out.println ("TestException");
try{
if (str.equals ("name")) {
System.out.println ("TestException");
}
}catch (Exception e) {
System.out.println (E.tostring ());
System.out.println (E.getmessage ());
}
}
}
Output Result:
; font-family: "microsoft=" "background-color:=" "/>Null

Example code 2:
public class TestInfo {
private static int m = 0;
public static Voidmain (string[] args) {
System.out.println ("TestException");
try {
m = 899/0;
} catch (Exception e) {
System.out.println (E.tostring ());
System.out.println (E.getmessage ());
       }
   }
}
Output Result:
Java.lang.ArithmeticException:/by Zero
/by Zero

Summary:
As you can see, the information obtained by e.tostring () includes the exception type and the exception detail message, while E.getmessage () just gets the verbose message string for the exception.

Differences between the Egetmessage () and ToString () methods in Java exception exception E

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.