栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【学习笔记】异常

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【学习笔记】异常

 异常:异常就是在程序的执行过程中产生的不正常的现象,它会中断程序的正常执行。
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];        //内存溢出

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/995987.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号