To create a custom exception in Java, extend the
Exception
class.Here's how you do it:
public class Test {
public static void main(String[] args) {
try {
throw new MyCustomException();
} catch (MyCustomException e) {
System.out.println("Custom exception got caught!");
}
}
}
// MyCustomException.java
public class MyCustomException extends Exception {
}