Java program to handle NoSuchMethodException
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 handle a NoSuchMethodException using the try, catch block. The code that may generate an exception should be written in the try block, and the catch block is used to handle the exception and prevent program crashes.
Program/Source Code:
The source code to handle NoSuchMethodException 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 a main() method. The main() method is the entry point for the program. Here, we created try and catch blocks. In the try block, NoSuchMethodException gets generated because we tried to access of sampleMethod() method, which is not present in the String class. And, we handled generated exceptions using the catch block and printed exception message.