java exception message

https:‮w//‬ww.lautturi.com
java exception message

In Java, an exception is an object that represents an error that occurred during the execution of a program. An exception includes a message that describes the error and other information about the exception.

To get the message of an exception in Java, you can call the getMessage() method of the Throwable class, which is the superclass of all exceptions. The getMessage() method returns a string that describes the error associated with the exception.

Here's an example of how to get the message of an exception in Java:

try {
    int x = 1;
    int y = 0;
    int z = x / y;
} catch (ArithmeticException ex) {
    String message = ex.getMessage();
    System.out.println(message);
}

In this example, an ArithmeticException is thrown when the code inside the try block attempts to divide by zero. The ArithmeticException is caught in the catch block and the exception's message is printed using the getMessage() method.

You can use the getMessage() method to get the message of any exception in Java. Just call the getMessage() method on the exception object.

Created Time:2017-11-03 15:57:13  Author:lautturi