The throw statement allows throwing any exception that a programmer deems necessary. One can even create their own exception. To create a checked exception, extend the java.lang.Exception class:
class MyCheckedException extends Exception{
public MyCheckedException(String message){
super(message);
}
//add code you need to have here
}
Also, to create an unchecked exception, extend the java.lang.RunitmeException class, as follows:
class MyUncheckedException extends RuntimeException{
public MyUncheckedException(String message){
super(message);
}
//add code you need to have here
}
Notice the comment add code you need to have here. You can add methods and properties to the custom exception as to any other regular class, but programmers rarely do it. The best practices even explicitly recommend avoiding using exceptions for driving the...