Java-based exception mechanism

 
Java-based exception mechanism

Classification of general anomalies

How to handle exceptions

How to print exceptions

Exception with inheritance

Custom exception class

Creation is not easy, if this blog is helpful to you, please remember to leave a message + like it.


Java has a very powerful exception handling mechanism. The ancestor of the exception is Throwable, which has the following two subclasses:

       Error: indicates an error. After the error occurs, the programmer cannot correct it by means of code, and can only be avoided in advance, just like life has stopped.

       Exception: indicates an exception. After the exception occurs, the programmer can correct it by means of code to make the program continue to run, which must be dealt with 

Exceptions are divided into compile-time exceptions and runtime exceptions .

Compile-time exceptions are also called compile-time monitored exceptions: during program compilation, non-runtime exceptions are detected, and the exceptions are either thrown upwards or caught.

Runtime exceptions (runtimeException and its subclasses): Runtime exceptions are detected during program compilation, and exceptions can be thrown directly on or inside the function, without capture and declaration. 

Classification of general anomalies

How to handle exceptions

     Throw an exception   throw  

     Declare exception   throws      up 

     Exception catch   try-catch   to handle it yourself

static  String[] usernames={"张三","赵四","王五"};
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入注册的名字!");
        String next = sc.next();
        //try-catch捕获异常
        try {
            checkUsername(next);
        } catch (RegisterException e) {
            e.printStackTrace();
        }
    }
    public static void checkUsername(String usrename) throws RegisterException {//声明异常
        for (String name : usernames) {
            if (name.equals(usrename)){
                throw new RegisterException("亲,该用户已经被注册!");//抛出异常
            }
        }
        System.out.println("恭喜您,注册成功!");
    }

How to print exceptions

     public String getMessage (): Get the description information of the exception and the reason (when prompted to the user, the reason of the error will be prompted.

     public String toString (): Get the type and description of the exception (not used).

     public void printStackTrace (): Print the abnormal trace stack information and output to the console

Exception with inheritance

If the parent class throws multiple exceptions, when the child class overrides the parent class method, it will throw the same exception as the parent class or a subclass of the parent class exception or not throw an exception.

The method of the parent class does not throw an exception, and the subclass cannot throw an exception when it overrides the method of the parent class. At this time, the subclass generates the exception and can only be caught and processed, and cannot be declared to be thrown

Custom exception class

Need to inherit Exception (compiler exception) or RunTimeException (runtime exception)

public class RegisterException extends Exception {

	public RegisterException() {
	}
	
	public RegisterException(String message){
	    super(message);
	}
}

Creation is not easy, if this blog is helpful to you, please remember to leave a message + like it.

Guess you like

Origin blog.csdn.net/promsing/article/details/112408071