Java program to get exception message using getMessage() method
belongs to collection: Java Exception Handling Programs
All Answers
total answers (1)
Severity: 8192
Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated
Filename: libraries/Filtered_db.php
Line Number: 23
belongs to collection: Java Exception Handling Programs
total answers (1)
In this program, we will use the getMessage() method to get an exception message in the catch block and print it.
Program/Source Code:
The source code to get the exception message using the getMessage() method is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created a class Main. The Main class contains the main() method.
The main() method is the entry point for the program. And, we generated ArrayOutOfBoundException in the try block and catch it in the catch exception. Then we get the exception message using the getMessage() method and printed the message.