Easy Tutorial
❮ Thread Getpri Method Enum1 ❯

Java Example - Custom Exception

Java Examples

The following example demonstrates how to implement a custom exception by extending the Exception class:

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"); // Throws custom class
    }
}
class TestInput {
    public static void main(String[] args){
        try {
            new Input().method();
        }
        catch(WrongInputException wie) {
            System.out.println(wie.getMessage());
        }
    } 
}

The above code outputs the following result when run:

Wrong input

Java Examples

❮ Thread Getpri Method Enum1 ❯