Java Examples - Custom Exception
The following example demonstrates implementing a custom exception by inheriting from Exception:
TestInput.java file
class WrongInputException extends Exception { //Custom class
WrongInputException(String s) {
super(s);
}
}
class Input {
void method() throws WrongInputException {
throw new WrongInputException("Wrong input"); //Throw the custom class
}
}
class TestInput {
public static void main(String[] args){
try {
new Input().method();
}
catch(WrongInputException wie) {
System.out.println(wie.getMessage());
}
}
}
The output of the above code is:
Wrong inputOther Extensions
Java Examples