【学习笔记】异常

 异常:异常就是在程序的执行过程中产生的不正常的现象,它会中断程序的正常执行。
1、异常处理:
(1)try:可以产生异常的代码
(2)catch:对异常捕获的代码

例:

     public static void main(String[] args) {        
        try {
        Object o=null;
        System.out.println(o.toString());
        System.out.println("try块最后的内容");
        }catch(NullPointerException e) {
            System.out.println("空指针异常");
        }     
        System.out.println("程序正常执行完成");
    }

//输出结果:

空指针异常
程序正常执行完成

(3)finally:无论是否引发都会执行的代码。除非程序遇到System.exit(0),才不会执行finally中的内容。

例1:

    public static void main(String[] args) {        
        try {
        Object o=null;
        System.out.println(o.toString());
        System.out.println("try块最后的内容");
        }catch(NullPointerException e) {
            System.out.println("空指针异常");
        }finally {
            System.out.println("finally中的代码");
        }     
        System.out.println("程序正常执行完成");
    }

//输出结果:

空指针异常
finally中的代码
程序正常执行完成

例2:

    public static void main(String[] args) {        
        try {
        Object o=null;
        System.out.println(o.toString());
        System.out.println("try块最后的内容");
        }catch(NullPointerException e) {
            System.out.println("空指针异常");
            System.exit(0);
        }finally {
            System.out.println("finally中的代码");
        }     
        System.out.println("程序正常执行完成");
    }

//输出结果:

空指针异常


2、注意:
(1)try块中产生的异常现象和catch块中捕获的异常类型必须对应上,如果不对应,相当于没有加入异常捕获,程序会终止。 

例:

try {
        Object o=null;
        System.out.println(o.toString());
        System.out.println("try块最后的内容");
        }catch(ArrayIndexOutOfBoundsException e) {        //数组下标越界
            System.out.println("空指针异常");
        }     
        System.out.println("程序正常执行完成");
    }

//由于不涉及数组越界,只是空指针异常,所以运行后还是报错,程序终止


(2)一个try块可以跟多个catch块,但必须先子类后父类。

 例:

    public static void main(String[] args) {    
        Scanner input=new Scanner(System.in);
        try {
            System.out.println("请输入第1个数:");
            int num1=input.nextInt();
            System.out.println("请输入第2个数:");
            int num2=input.nextInt();    
            int result=num1/num2;
            System.out.println("结果是:"+result);
        }catch(InputMismatchException e) {
            System.out.println("您输入不合法");            
        }catch(ArithmeticException e) {
            System.out.println("以0作除数");
        }catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            System.out.println("finally中的代码");
        }     
        System.out.println("程序正常执行完成");
    }

//输出结果(示例):

请输入第1个数:
5
请输入第2个数:
0
以0作除数
finally中的代码
程序正常执行完成


(3)结构:try..catch      try...finally      try..catch..finally
(4)throws:声明方法中可能产生的异常
(5)throw:  抛出异常

例:

//SexException.java文件

public class SexException extends Exception{
    public SexException() {
        System.out.println("性别只能是男或女");
    }
}

//Test.java文件

    public static void main(String[] args) {            
        try {
            print();
        }catch(InputMismatchException e) {
            System.out.println("您输入不合法");            
        }catch(ArithmeticException e) {
            System.out.println("以0作除数");
        }catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            System.out.println("finally中的代码");
        }     
        System.out.println("程序正常执行完成");
    }
    public static void print() throws InputMismatchException,
    ArithmeticException,SexException{
        Scanner input=new Scanner(System.in);
        System.out.println("请输入性别:");
        String sex=input.next();
        if(!"男".equals(sex)&&!"女".equals(sex)) {
            throw new SexException();
        }
        System.out.println("请输入第1个数:");
        int num1=input.nextInt();
        System.out.println("请输入第2个数:");
        int num2=input.nextInt();    
        int result=num1/num2;
        System.out.println("结果是:"+result);
    }


3、异常类:所有异常的类的根类是Exception类
(1)Throwable类是Exception类的父类,还有另一个子类的Error。Error代表的是错误,错误可以是内存溢出。不能通过捕获避免,必须修改代码。

例:        int[] nums=new int[9999999999999];        //内存溢出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值