Java NoSuchFieldException
Example
Look for a field that does not exist, and catch the exception:
public class Main {
public static void main(String[] args) {
try {
Main.class.getField("x");
} catch (NoSuchFieldException e) {
System.out.println("The field was not found.");
}
}
}
Definition and Usage
A NoSuchFieldException happens when a program looks for a field (a variable that belongs to a class) by its name, as text, and the class has no public field with that name.
In the example, Main.class gives information about the Main class, and getField("x") looks for a field named x. 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