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