Untitled
🧩 Syntax:
class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}
public class CustomExceptionExample {
public static void main(String[] args) {
try {
// Simulating a situation where the custom exception is thrown
throwCustomException();
} catch (CustomException e) {
// Catching and handling the custom exception
System.out.println("Caught CustomException: " + e.getMessage());
}
}
// Method that throws the custom exception
private static void throwCustomException() throws CustomException {
// Simulating a condition that triggers the custom exception
boolean errorCondition = true;
if (errorCondition) {
throw new CustomException("This is a custom exception message.");
}
}
}