1

Java exception types and handling

Foreword: The exception refers to an abnormal situation during the execution of the program, which causes the java of jvm to stop.

abnormal structure of is 1611bcefe4e430:

Throwable is the top parent class

  • Subclass Error is a serious error,
  • The subclass Exception is what we call abnormal

Keywords for exception handling

java five keywords exception handling: the try, the catch, a finally, the throw, throws

throw throws an exception, thorws declares an exception, catches the exception try_catch


throw

public class SegmentFault {
    public static void main(String[] args) {

        /**
         *  throw 抛出异常
         *    格式 - throw new 异常类名(参数);
         * */

        // 创建一个数组
        int [] arr = { 2, 4, 56 ,5};
        // 根据索引找到对应的元素
        int index = 4;
        int element = getElement(arr,index);
        System.out.println(element);
        System.out.println("owo"); // 运行错误 无法继续
    }
        /** throw 抛出异常 提醒你必须处理  */
    public static int getElement(int [] arr, int index){
        // 判断数组索引是否越界
        if (index < 0  || index > arr.length -1){
            /**
             * 条件满足越界 当执行到throw抛出异常后就无法运行,结束方法并且提示
             * */
            throw new ArrayIndexOutOfBoundsException("数组下标越界异常");
        }
        int element = arr[index];
        return element;
    }
}    

abnormal result of is 1611bcefe4e599:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of bounds exception


throws

public class SegmentFault{
    public static void main(String [] args){
    
        read("a.txt");
        
    }
      public static void read(String path) throws FileNotFoundException, IOException {
        if (!path.equals("a.txt")){  // 如果没有a.txt
            // 如果不是 a.txt 该文件不存在 是一个错误 也就是异常 throw
            throw new FileNotFoundException("文件不存在");
        }
        if (!path.equals("b.txt")){
            throw new IOException("文件不存在");
        }
    }
    
}

abnormal result of is 1611bcefe4e601:

Exception in thread "main" java.io.IOException: File does not exist


try, catch, finally + Throwable common methods.

Throwable common methods are as follows
printStackTrace() : * Print exception details.
getMessage() : Obtain the cause of the exception.
toString(): Obtain the exception type and description information.


public class Demo03 {
    public static void main(String[] args) {

        /**
         *  try- catch  捕获异常
         * */


        // 可能会生成的异常
        try {     // 捕获或者声明
            read("b.txt");
        } catch (FileNotFoundException e) {   // 使用某种捕获,实现对异常的处理
            System.out.println(e);
            /**
             *  Throwable中的查看方法
             *  getMessage 获取异常信息  提示给用户看的
             *  toString   获取异常的类型和异常描述(不用)
             *  printStackTrace
             * */
            
            
            System.out.println("Throwable常用方法测试");
            System.out.println(e.getMessage()); // 文件不存在
            System.out.println(e.toString());
            e.printStackTrace();
            
            

        } finally {
            System.out.println("不管程序怎样,这里都会被执行");
        }

        System.out.println("over");

    }

    public static void read(String path) throws FileNotFoundException {
        if (!path.equals("a.txt")) {
            throw new FileNotFoundException("文件不存在");
        }
    }
    
}

output of

java.io.FileNotFoundException: File does not exist
-----Throwable common method test ------
file does not exist
java.io.FileNotFoundException: File does not exist
No matter what the program is, it will be executed here
over


Note: try catch finally can not be used alone


嘻嘻硕
27 声望12 粉丝

想当一只天然呆的鸭qwq