JavaSE学习:第二十一章、异常

1、定义:

1、异常(Exception)就是在程序的运行过程中出现的不正常现象导致了程序中断。
2、异常(Exception) ≠ 错误(error)
3、在java中, 把一些不正常现象进行了抽象, 形成了一些异常类。

在这里插入图片描述

2、常见异常:

1.空指针异常: NullPointerException
2.类型转换异常:ClassCastException
3.算术异常: ArithmeticException

在这里插入图片描述

3、异常处理机制:

在这里插入图片描述

快捷键:Ctrl+Alt+t
try catch finally

try {
	可以发生异常的代码块
} catch(Exception e) {
	1、捕获异常。
	2、将异常封装到Exception对象e,传递给catch3、得到异常后,程序员自己处理。
	4、如果没有发生异常,catch不执行。
} finally {
	1、不管是否发生异常,finally一定会执行。
	2、因此通常将释放资源的代码写在这里。
}
public class Exception01 {
    public static void main(String[] args) {
        try {
            String name = "hsf";
            int num = Integer.parseInt(name);
            System.out.println("异常后面的代码");  // 1、异常后面的代码不执行
        } catch (NumberFormatException e) {
            System.out.println(e.getMessage());  ///2、直接跳入执行catch里面代码
        } finally {
            System.out.println("我是finally里面的代码"); ///3、finally代码执行
        }
        System.out.println("helloWord");  // 4、输出后面代码
    }
}

可以多个catch同时使用:但是要把Exception的子类放在前面。

package com.hsf.exception;/*
 * @author HSF
 * @version 8.0
 */

import javax.sound.midi.Soundbank;
import java.util.Scanner;

public class Exception01 {
    public static void main(String[] args) {
        people people = new people();
        people = null;
        
        int num1 = 10;
        int num2 = 0;
        try {
            System.out.println(people.name);
            int result = num1 / num2;
        }catch (NullPointerException e) {
            System.out.println(e.getMessage());
        } catch (ArithmeticException e) {
            System.out.println(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class people {
    public String name = "jack";
}

可以进行try-finally搭配使用,这种写法相当于没有捕获异常,因此程序如果发生了异常会直接退出。应用场景:执行一段代码,不管是否发生异常,都必须执行某个业务逻辑。

throws

在这里插入图片描述

运行时异常有默认的throws,可以不用处理;编译时异常必须要处理,没有默认throws
在这里插入图片描述

在这里插入图片描述


在这里插入图片描述

    public void f1() throws Exception {
        
    }
    public void f2() throws NullPointerException, ArrayIndexOutOfBoundsException {

    }

3、自定义异常:

package com.hsf.exception;/*
 * @author HSF
 * @version 8.0
 */

import javax.sound.midi.Soundbank;
import java.security.PublicKey;
import java.util.Scanner;

public class Exception01 {
    public static void main(String[] args) {
        int age =  130;
        if(!(age >= 18 && age <= 120)) {
            // 通过构造器设置错误提示信息
            throw new AgeException("年龄要在18~120之间");
        }
        System.out.println("年龄范围正确");
    }
}

class AgeException extends RuntimeException {  // 一般继承RuntimeException 
    // 使用父类的构造器
    public AgeException(String message) {
        super(message);
    }
}

在这里插入图片描述

在这里插入图片描述

作业:

try里面的异常会等到finally执行完毕后再来执行。

package com.hsf.exception;/*
 * @author HSF
 * @version 8.0
 */

public class Test {
    public static void main(String[] args) {
        try {
            Except.methodA();
        } catch (Exception e) {

        }
        Except.methodB();
    }
}
class Except {
    static void methodA() {
        try {
            System.out.println("进入方法A");  // 第一句输出
            throw new RuntimeException("制造异常");  // 第三句输出
        } finally {
            System.out.println("A方法里面的finally");  // 第二句输出
        }
    }

    static void methodB() {
        try {
            System.out.println("进入方法B");
            return;
        } finally {
            System.out.println("B方法里面的finally");
        }
    }
}

作业2:

package com.hsf.exception;/*
 * @author HSF
 * @version 8.0
 */

import com.sun.xml.internal.ws.api.ha.StickyFeature;

public class Test2 {
    public static void main(String[] args) {
        try {
            if(args.length != 2) {
                throw new ArrayIndexOutOfBoundsException("参数个数不对");
            }
            int n1 = Integer.parseInt(args[0]);
            int n2 = Integer.parseInt(args[1]);

            double res = cal(n1, n2);
            System.out.println(res);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("数组越界");
        } catch (NumberFormatException e) {
            System.out.println("参数格式不正确");
        } catch (ArithmeticException e) {
            System.out.println("除0异常");
        }
    }

    public static double cal(double n1, double n2) {
        return n1 / n2;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值