基础3-高级-异常类

概述:

概述:导致程序的正常流程被中断的事件,叫做异常 。
Throwable
            
      1、可查异常(Exception):必须进行处理的异常。
      2、不可查异常
            a、运行时异常(RuntimeException):下标越界、空指针异常、除数不为0。
            b、错误  (Error):系统级别异常,程序无法处理。


//Throwable常用方法
package exercise;
public class exercise {
    public static void main(String[] args){
        System.out.println(new Throwable().getMessage());//获取异常信息
        System.out.println(new Throwable().toString());//打印异常类名
        new Exception().printStackTrace();//jvm默认用这种方式处理异常
    }
}

异常产生原因分析


基础3-高级-异常类_第1张图片
图片.png

throw关键字

它可以在指定的方法抛出指定的异常
格式:throw new xxxException("异常产生的原因");
注意:
1、throw关键字必须写在方法内部
2、throw关键字后边的new的对象必须是Exception与子对象
3、throw关键字抛出的异常必须处理,除了RuntimeException异常
代码演示:
package com.llhc;
public class Hello {
    public static void main(String[] args) {
        int [] arr = null;
        int a =getElement(arr,0);
        System.out.println(a);
    }

    private static int getElement(int[] arr, int i) {
        //如果数组是空,抛出异常
        if(arr == null){
            throw new NullPointerException("数组是空");
        }
        int ele=arr[i];
        return ele;
    }
}

throws

异常处理的第一种方式
作用:当throw抛出异常,就用throws处理
格式:
public  void  run() throws  xxxException{
      throw new xxxException("抛出异常");
}
注意:
1、throws必须写在方法声明处
2、throws后面必须是Exception与子类
3、抛出多少,throws就声明多少,如果是子父级关系,只抛出父
4、抛出后交给JVM处理,要么用try  catch处理
代码:
package com.llhc;
import java.io.FileNotFoundException;
public class Hello {
    public static void main(String[] args) throws FileNotFoundException {//我们调用了申明抛出异常的方法,我们就要申明抛出
        getElement("c:\\b.txt");
    }

    private static void getElement(String i) throws FileNotFoundException {//申明抛出交给jvm处理
        if(! i.equals("c:\\a.txt")){
            throw new FileNotFoundException("没这个路径");//抛出
        }
    }
}

try catch

自己处理异常
格式:
try{
    可能产生异常
}catch(处理逻辑){
    处理方案
}finally{
    释放资源
}
代码演示:
package com.llhc;
public class Hello {
      /*
     * try:用来检测异常的
     * catch:用来捕获异常的
     * finally:释放资源
     * */
    public static void main(String[] args) {
        Run run = new Run();
        try{
            int num=run.run1(10,0);
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("-----分---隔---线---");
            System.out.println("分母不能为0");//提示
            System.out.println("打印异常信息:"+e.getMessage());//打印异常信息
            System.out.println("打印异常类名:"+e.toString());//打印异常类名
        }   
    }
}
class Run{
    public int run1(int a,int b){
        return a/b;
    }
}

finally关键字

1、不能单独使用,必须和try一起使用
2、无论程序是否有异常,它都要释放
3、只有关闭jvm时,finally才停止执行。

异常注意事项


基础3-高级-异常类_第2张图片
image.png

面试题

JDK是如何处理多个异常
对于多个异常的处理,jdk往往会这样做:
catch(ArithmeticException | ArrayIndexOutOfBoundsException e1){
            System.out.println("错了");
}

面试题


基础3-高级-异常类_第3张图片
image.png

面试题

1、final和finally和finalize的区别
Final可以修饰类,方法,变量
Finally是try语句中的语句体,不能单独使用,用来释放资源
Finalize是方法,由对象回收器调用此方法。
2、如果catch里面有return语句,请问finally的代码还会执行吗?如果会,请问是在return前还是return后。
会,return前。

自定义异常

自定义为了区分子类异常名字。
//自定义异常
/*
 * 通过名字区分到底是什么异常
 * 有针对的解决办法。
 * */
class UsersException extends Exception{
    
}
/*
 * 
 * 继承运行时异常
 * */
class UserException extends RuntimeException{
    
}

你可能感兴趣的:(基础3-高级-异常类)