Java NoSuchMethodException
Example
Look for a method that does not exist, and catch the exception:
public class Main {
public static void main(String[] args) {
try {
Main.class.getMethod("x");
} catch (NoSuchMethodException e) {
System.out.println("The method was not found.");
}
}
}
Definition and Usage
A NoSuchMethodException happens when a program looks for a method by its name, as text, and the class has no matching public method.
In the example, Main.class gives information about the Main class, and getMethod("x") looks for a method named x with no parameters. It is a checked exception (Java requires you to catch it, or to declare it with throws).
Related Pages
Java Exceptions (try...catch) Tutorial
❮ Errors & Exceptions