19.异常处理(新年快乐啊)

本文详细讲解了Java中throw关键字用于在方法内部抛出运行时异常,以及throws关键字在方法声明中声明可能抛出的异常,通过实例演示了如何捕获和处理异常。重点介绍了常见异常如越界和空指针,以及如何在代码中正确使用try-catch-finally结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.抛出异常throw

断言略

对可恢复的错误不能使用断言,而应该抛出异常;

断言很少被使用,更好的方法是编写单元测试。

1.1 概念

当使用方法时,需要告诉调用者出错的信息。我们这时候可以使用throw

1.2举例

需求:编写一个通过数组下标获取数组,如果下标越界,在控制台打印“亲,下标越界了”,如果数组为空,在控制台打印“亲,你的数组为空!!!”       ;

  public static int getIndexArray( int[] arr,int index) {

        if(arr == null) {

            //

            throw new NullPointerException("亲,你的数组为空了!!!");

        }

        if( index < 0 || index > (arr.length-1)  ) {

            throw new ArrayIndexOutOfBoundsException("亲,你的下标越界了!!!");

        }

        return arr[index];

    }

执行:

  public static void main(String[] args) throws ParseException {

        int[] arr = null;

        int i = getIndexArray(arr,2);

}

结果:

    public static void main(String[] args) throws ParseException {

        int[] arr = {1,2,3};

        int i = getIndexArray(arr,3);

}

2. 捕获异常try…catch...finally(可选择)

throws描述:

如果一个方法(中的语句执行时)可能生成某种异常,但是并不能确定如何处理这种异常,则此方法应显式地声明抛出异常,表明该方法将不对这些异常进行处理,而由该方法的调用者负责处理。

在方法声明中用 throws 子句可以声明抛出异常的列表,throws后面的异常类型可以是方法中产生的异常类型,也可以是它的父类。

Throws 举例:

需求: 定义读取log.txt,如果文件不存在就抛给调用者,让调用者处理文件不存在的异常。

  public static void read(String fileName) throws FileNotFoundException {

        if(!"log.txt".equals(fileName)) {

            throw new FileNotFoundException();

        }

    }

调用:

    public static void main(String[] args) throws FileNotFoundException {

        read("111");

    }

举例:

Exception in thread "main" java.io.FileNotFoundException

    at cn.hzat.Demo1.read(Demo1.java:24)

    at cn.hzat.Demo1.main(Demo1.java:20)

注意字符串判断!!

  //需求: 定义读取log.txt,如果文件不存在就抛给调用者,让调用者处理文件不存在的异常。

   

    public void read(String fileName) {

       

        /*

         不推荐使用

        if(!fileName.equals("log.txt")) {

           

        }

        */

        // 如果传的文件不是log.txt 说明读取日子文件失败

        if(!"log.txt".equals(fileName)) {

           

        }

       

    }

3. 声明异常throws

使用情况说明:如果异常出现的话,会立刻终止程序,所以我们得处理异常,如果我们不想终止可以自己处理异常

4.throw和throws

package com.hzit.demo1;

import java.io.FileNotFoundException;

/**

*

* throw: 作用在方法里面 如果抛出运行时期异常,调用者不要处理 并且不要配合throws使用

* 如果抛出编译时期异常,需要配合throws使用 ,调用者必须处理

* throws: 作用在方法上 抛出异常 调用者必须处理 可以单独使用

* 面试题

*

*/

public class TextLoad {

// * 定义读取log.txt,如果文件不存在就抛给调用者,让调用者处理文件不存在的异常。

public void loadFile(String fileName) throws FileNotFoundException {

if(!"log.txt".equals(fileName)){

// throw 编译时期异常,告诉调用报错,调用必须处理

throw new FileNotFoundException("找不到对应的文件。");

}

}

public void loadFile1(String fileName) throws FileNotFoundException {

return;

}

}

package com.hzit.demo1;

import java.io.FileNotFoundException;

/**

* 定义读取log.txt,如果文件不存在就抛给调用者,让调用者处理文件不存在的异常。

*

*

* throws FileNotFoundException

* : 当前代码处理不了 交给调用者进行处理

*

* throws 当你抛给jvm处理的时候 程序断开,后面的代码不会执行

*/

public class Demo2 {

public static void main(String[] args) throws FileNotFoundException { // 继续抛异常,交给jvm处理

TextLoad textLoad = new TextLoad();

textLoad.loadFile("XXX");

System.out.println("代码......");

System.out.println("代码......");

System.out.println("代码......");

System.out.println("代码......");

System.out.println("代码......");

}

}

package com.hzit.demo1;

import java.io.FileNotFoundException;

/**

* 定义读取log.txt,如果文件不存在就抛给调用者,让调用者处理文件不存在的异常。

*

*

* throws FileNotFoundException

* : 当前代码处理不了 交给调用者进行处理

*

* throws 当你抛给jvm处理的时候 程序断开,后面的代码不会执行

*

* try{

* // 存放可能会出现异常的代码 不管有没有发送异常都会执行

* } catch (FileNotFoundException e) {

* // 发送异常会执行的的代码,如果代码没问题 , 不会执行

* }

*

*

*

*/

public class Demo3 {

public static void main(String[] args) { // 继续抛异常,交给jvm处理

TextLoad textLoad = new TextLoad();

try {

textLoad.loadFile("log.txt");

} catch (FileNotFoundException e) {

e.printStackTrace();

System.err.println("代码出错");

}finally {

// 不管异常是否发送都会执行的代码

System.out.println("我一定会执行!!!!");

}

System.out.println("代码......");

System.out.println("代码......");

System.out.println("代码......");

System.out.println("代码......");

System.out.println("代码......");

}

}



System.err.println();

5.常见异常举例

越界

空指针异常---null不能调用API

/0

编码中异常处理代码位置有两种选择:

1.上抛

public static void main(String[] args) throws IOException {

2.旁边抛try/catch

原则:先抛子类,后抛大类,否则会短路。顺序问题还会引起阻塞

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值