Day08 Java se exception mechanism

Keywords: Java network jvm

Java se exception mechanism

1, What is anomaly?

Exception refers to various unexpected situations in the program
Such as: file not found, network connection failure, illegal parameters, etc.

  • Simple classification
    • Checking exception: an exception caused by a user error or problem that cannot be foreseen.
    • Runtime exception: can be caught or not handled
    • Error ERROR
  • Java treats exceptions as objects. Superclass: java.lang.Throwable
    • Error: the JVM generally chooses to terminate the thread.
    • Exception: handle exceptions as much as possible in the program.

2, Exception handling mechanism

1 catch exception

Use code block

try(){//try monitoring area

}catch(){// Catch (the type of exception you want to catch) catches the exception

}finally{ //When dealing with aftermath work, it can not be finally used in IO, resource shutdown and other places, indicating that it is always executed.

}

You can use the shortcut key: command+alt+T

  • Case demonstration 1: (1 / 0 exception capture)
package com.exception;

public class Demo01 {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        try { //try monitoring area
            System.out.println(a/b);
        }catch (ArithmeticException e){ //Capture exception
            System.out.println("Program exception! variable b Can't be zero.");
        }finally { //Deal with the abnormal after treatment
            System.out.println("finally");
        }
    }
}
  • Case 2: (multiple exception capture)
package com.exception;
//Exception capture should be from small to large, not from large to small, otherwise it will be overwritten!
public class Demo01 {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        try { //try monitoring area
            System.out.println(a/b);
        }catch (Error e){
            System.out.println("Error");
        }catch (Exception e){
            System.out.println("Exception");
        }catch (Throwable e){ //Capture exception
            System.out.println("Throwable");
        }finally { //Deal with the abnormal after treatment
            System.out.println("finally");
        }
    }
}

2 throw an exception

  • Notice the difference between throw and throws
package com.exception;

public class Demo01 {
    public static void main(String[] args) {
        try {
            new Demo01().test(1,0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //If the exception cannot be handled in this method, an exception will be thrown on the method.
    public void test(int a, int b) throws ArithmeticException{
        if (b==0){ //Notice the difference between throw and throws
            throw new ArithmeticException();//Actively throw exceptions, usually used in methods
        }
    }
}

3 custom exception

You can define exceptions by yourself, just inherit the Exception class.

Case presentation:

package com.exception;

//Custom exception class: receive data + Print exception information
public class MyException extends Exception{

    //Pass array: if the number is greater than 10, an exception is thrown
    private int detail;

    public MyException(int a) {
        this.detail = a;
    }

    //toString: abnormal printing information
    @Override
    public String toString() {
        return "MyException{"+ detail +
                '}';
    }
}
package com.exception;
//Test class
public class Test {
    //There may be abnormal methods

    static void test(int a ) throws MyException{
        System.out.println("The parameters passed are: "+ a);
        if (a>10){
            throw new MyException(a);//Define when to throw
        }
        System.out.println("OK!");
    }

    public static void main(String[] args) {
        try {
            test(20);
        } catch (MyException e) {
            System.out.println("MyException=>"+e);
        }
    }
}

Conclusion:

  • When handling runtime exceptions, logic is used to avoid them reasonably and assist try catch processing.
  • After multiple catch blocks, you can add a catch(Exception) to handle possible missing exceptions.
  • For uncertain code, try catch can also be added.
  • Try to add a finally statement to release the occupied resources. IO~ Scanner~

Written in the end

Think of a gift when you are in someone else's life!

To Demut and Dottie!

13 original articles published, 14 praised, 743 visited
Private letter follow

Posted by integravtec on Tue, 03 Mar 2020 01:32:38 -0800