directory

  • Learning materials
  • abnormal
  • Exception handling
  • Custom exception

Learning materials

B station crazy god said: www.bilibili.com/video/BV12J…

abnormal

Program due to user input error… Program error occurs, terminate.

Check exception: syntax error

Runtime exception: program run time occurrence: subscript out of bounds, arithmetic exception, null pointer exception

Mistakes: Mistakes can be fatal

Exception handling

Try, catch, finally, throw, throws (method throws)

package com.zy7y.exceptstudy;

/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.exceptstudy
 * @Author: zy7y
 * @Date: 2020/8/15 4:59 PM *@Description: * /
public class Demo1 {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        // Try catch must have finally optional
        try{
            // If an exception occurs, execute the contents of the catch block
            System.out.println(a/b);
            throw new RuntimeException();
            // Exception Specifies the type of Exception caught
        }catch (Exception e) {
            System.out.println("Abnormal hair happened!");
        }catch (Error e){
            System.out.println("Error occurred.");
        }catch (Throwable t){
            System.out.println("Exception/error parent occurred");
            // CTRL + Alt + T + selected code can quickly generate some code
        } finally {
            System.out.println("The result will be carried out no matter what.");
        }

        // throw new exception class (); Actively throws an exception that is used in the method}}Copy the code

Custom exception