Creating Custom Exceptions
You can create your own exceptions by extending the Exception
class.
class MyException extends Exception {
public MyException(String message) {
super(message);
}
}
public class Main {
public static void main(String[] args) {
try {
throw new MyException("Custom error!");
} catch (MyException e) {
System.out.println("Caught: " + e.getMessage());
}
}
}
This is useful when the standard exceptions don't fit your use case.