Custom exceptions provide a way to create your own exception types for specific error cases, improving error handling clarity.
Source Code
class MyException extends Exception {
public MyException(String message) {
super(message);
}
}
public class Main {
public static void main(String[] args) {
try {
throw new MyException("Custom Message");
} catch (MyException e) {
System.out.println(e.getMessage());
}
}
}