Junior — Middle
Is it possible to identify a user-defined exception that does not require mandatory handling?
sobes.tech AI
Answer from AI
In Java, you can define a custom exception that does not require mandatory handling by creating a class that extends RuntimeException. Such exceptions are called unchecked exceptions and do not require mandatory catching or declaration in the method signature.
Example of a custom unchecked exception:
public class MyUncheckedException extends RuntimeException {
public MyUncheckedException(String message) {
super(message);
}
}
Usage:
throw new MyUncheckedException("Error without mandatory handling");
Thus, a developer can create exceptions that can be thrown without the need to wrap them in try-catch blocks or declare them in throws.