Java Exception Using GetMessage()
Chapter:
Exception Handling
Last Updated:
05-11-2016 14:22:40 UTC
Program:
/* ............... START ............... */
public class JavaExceptionMessageUsingGetMessage {
public static void main(String[] args) {
try {
int x = 1 / 0;
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
/* ............... END ............... */
Output
Notes:
-
The java.lang.Throwable.getMessage() method returns the detail message string of this throwable.
- getMessage() is a method of Throwable class (super class of all exceptions of Java) inherited by every exception class like ArithmeticException as in the above code. The getMessage() method prints only the message part of the output printed by object e. This style can be preferred when the programmer would not like to give his message to the actual user.
Tags
Exception Using GetMessage() , Java, Exception