Checked and unchecked of Java exceptions

First of all, java exceptions are divided into Error and Exception. Both of these classes are subclasses of the interface Throwable. The relationship between Error and Exception and their subclasses can be roughly described in the following figure.



Notes:

1. Error only occurs in the Java virtual machine, and the user does not need to catch or throw Error in the program.

2. Exception is divided into two categories: general Exception and RuntimeException. A bit awkward here is that RuntimeException (Unchecked) inherits from the parent class of Exception (Checked).

PS: Understanding of the concepts of checked and unchecked:

checked: generally refers to the external conditions that the program cannot directly control, and refers to a type of exception that needs to be checked during compilation. The user program must use the try catch mechanism to handle it or pass it through throws. The caller handles it. This type of exception mainly refers to exceptions other than Error and RuntimeException and their subclasses.

unchecked: refers to a class of exceptions that do not need to be handled at compile time. In the java system, all Error and RuntimeException and their subclasses are unchecked exceptions. It can be understood directly as an exception that does not need to be handled by mechanisms such as try catch, which can be considered as an unchecked exception.

Checked and unchecked are reflected in the following figure in the inheritance relationship of throwable:

+-----------+
                 | Throwable |
                 +-----------+
                  /         \
                 /           \
          +-------+          +-----------+
          | Error |          | Exception |
          +-------+          +-----------+
           /  |  \           / | \        \
          \________/       \______/         \
                                        +------------------+
          unchecked        checked      | RuntimeException |
                                        +------------------+
                                         /   |    |      \
                                        \_________________/
                        
                                            unchecked


First define a basic exception class GenericException, inherited from Exception.
package check_unchecked_exceptions;

public class GenericException extends Exception{

    /**
     *
     */
    private static final long serialVersionUID = 2778045265121433720L;
    
    public GenericException(){
        
    }
    
    public GenericException(String msg){
        super(msg);
    }
}


The following defines a test class VerifyException

package check_unchecked_exceptions;

public class VerifyException {

    public void first() throws GenericException {
        throw new GenericException("checked exception");
    }
    
    public void second(String msg){
        if(msg == null){
            throw new NullPointerException("unchecked exception");
        }
    }
    
    public void third() throws GenericException{
        first();
    }
    
    public static void main(String[] args) {
        VerifyException ve = new VerifyException();
        
        try {
            ve.first();
        } catch (GenericException e) {
            e.printStackTrace ();
        }

        ve.second(null);
    }

}

In the above example, combined with the concepts of checked and unchecked, it can be seen that the parent class of Exception is a checked type, but its subclass RuntimeException (subclass NullPointerException) is unchecked.
https://www.cnblogs.com/shihuc/p/5201905.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326291235&siteId=291194637