Java常用工具——java异常

package com.imooc.exception;

import java.util.Scanner;

public class TryCatchDemo1 {

    public static void main(String[] args) {
        // 要求输入两个数,输出两数之商
        Scanner input=new Scanner(System.in);
        System.out.println("========运算开始==========");
        try {
            System.out.println("请输入第一个整数:");
            int one=input.nextInt();
            System.out.println("请输入第二个整数:");
            int two=input.nextInt();
            System.out.println("one和two的商是:"+(one/two));
        }catch(Exception e) {
            System.out.println("输入内容错误!");
        }
        
        System.out.println("========运算结束==========");
        

    }

}

二、try-catch-finally

1、简单处理异常

2、打印出错信息

3、finally处理善后

package com.imooc.exception;

import java.util.Scanner;

public class TryCatchDemo1 {

    public static void main(String[] args) {
        // 要求输入两个数,输出两数之商
        Scanner input=new Scanner(System.in);
        System.out.println("========运算开始==========");
        try {
            System.out.println("请输入第一个整数:");
            int one=input.nextInt();
            System.out.println("请输入第二个整数:");
            int two=input.nextInt();
            System.out.println("one和two的商是:"+(one/two));
        }catch(Exception e) {
            //1、处理错误,简单输出提示信息
            System.out.println("输入内容错误!");
            //2、打印错误信息
            e.printStackTrace();
        }finally {
            //3、finally块释放资源
            System.out.println("========运算结束==========");
        }
        
        
        

    }

}

三、try-catch-catch-finaly

1、对不同类别的异常分别处理

2、最后增加异常父类Exception

package com.imooc.exception;

import java.util.InputMismatchException;
import java.util.Scanner;

public class TryCatchDemo1 {

    public static void main(String[] args) {
        // 要求输入两个数,输出两数之商
        Scanner input=new Scanner(System.in);
        System.out.println("========运算开始==========");
        try {
            System.out.println("请输入第一个整数:");
            int one=input.nextInt();
            System.out.println("请输入第二个整数:");
            int two=input.nextInt();
            System.out.println("one和two的商是:"+(one/two));
        }catch(ArithmeticException e) {
            //1、算术异常处理
            System.out.println("除数不能为零!");
            //2、打印错误信息
            e.printStackTrace();
        }catch(InputMismatchException e) {
            //3、输入异常处理
            System.out.println("请输入整数!");
            e.printStackTrace();
            
        }catch(Exception e) {
            //4、其他错误异常处理
            System.out.println("程序出错了!");
            e.printStackTrace();
        }
        finally {
            //3、finally块释放资源
            System.out.println("========运算结束==========");
        }
        
        
        

    }

}

四、return语句

package com.imooc.exception;

import java.util.InputMismatchException;
import java.util.Scanner;

/*
 * 带return语句的异常处理
 */
public class TryCatchRetrun {

    public static void main(String[] args) {
        TryCatchRetrun tcr=new TryCatchRetrun();
        int result=tcr.test();
        System.out.println("两个数的商是:"+result);
        

    }
    public static int test() {
        Scanner input=new Scanner(System.in);
        System.out.println("========运算开始==========");
        try {
            System.out.println("请输入第一个整数:");
            int one=input.nextInt();
            System.out.println("请输入第二个整数:");
            int two=input.nextInt();
            return one/two;
        }catch(ArithmeticException e) {
            //1、算术异常处理
            System.out.println("除数不能为零!");
            return 0;
        }finally {
            //3、finally块释放资源
            System.out.println("========运算结束==========");
            //finally中的return语句会屏蔽try和catch块中的return语句,所以不建议在finally中加入return语句
            //return -10000;
        }
    }

}

五、throws声明异常

1、throws后面接多个异常类型,中间用逗号分隔

package com.imooc.exception;


import java.util.InputMismatchException;
import java.util.Scanner;

/*
 * 1、throws声明多种异常类型,但不处理异常
 * 2、由调用者针对不同异常类型,进行处理
 */
public class ThrowsDemo {

    public static void main(String[] args) {
        
        try {
            int result=test();
            System.out.println("两个数的商是:"+result);
        }catch (ArithmeticException e) {
            // 处理算术异常
            System.out.println("除数不能为零!");
            e.printStackTrace();
        }catch (InputMismatchException e) {
            // 处理输入异常
            System.out.println("必须输入整数!");
            e.printStackTrace();
        }
        

    }
    public static int test() throws ArithmeticException,InputMismatchException {
        Scanner input=new Scanner(System.in);
        System.out.println("========运算开始==========");
        System.out.println("请输入第一个整数:");
        int one=input.nextInt();
        System.out.println("请输入第二个整数:");
        int two=input.nextInt();
        System.out.println("========运算结束==========");
        return one/two;
        
    }

}

2、throws后面接Exception

package com.imooc.exception;


import java.util.InputMismatchException;
import java.util.Scanner;

/*
 * 1、throws后面接Exception
 */
public class ThrowsDemo2 {

    public static void main(String[] args) {
        
        try {
            int result=test();
            System.out.println("两个数的商是:"+result);
        }catch (ArithmeticException e) {
            // 处理算术异常
            System.out.println("除数不能为零!");
            e.printStackTrace();
        }catch (InputMismatchException e) {
            // 处理输入异常
            System.out.println("必须输入整数!");
            e.printStackTrace();
        }catch(Exception e) {
            System.out.println("程序出错误了!");
            e.printStackTrace();
        }
        

    }
    public static int test() throws Exception {
        Scanner input=new Scanner(System.in);
        System.out.println("========运算开始==========");
        System.out.println("请输入第一个整数:");
        int one=input.nextInt();
        System.out.println("请输入第二个整数:");
        int two=input.nextInt();
        System.out.println("========运算结束==========");
        return one/two;
        
    }

}

3、为非检查异常增加文档注释

Exception为检查异常,声明后,编译器会提示必须处理;

ArithmeticException,InputMismatchException为非检查异常,编译器不会强制要求,可以使用文档注释进行提示

package com.imooc.exception;


import java.util.InputMismatchException;
import java.util.Scanner;


public class ThrowsDemo2 {

    public static void main(String[] args) {
        
        try {
            int result=test();
            System.out.println("两个数的商是:"+result);
        }catch (ArithmeticException e) {
            // 处理算术异常
            System.out.println("除数不能为零!");
            e.printStackTrace();
        }catch (InputMismatchException e) {
            // 处理输入异常
            System.out.println("必须输入整数!");
            e.printStackTrace();
        }catch(Exception e) {
            System.out.println("程序出错误了!");
            e.printStackTrace();
        }
        test();

    }
    /**
     * 测试接收数据相除结果的方法
     * @return 两个数据的商
     * @throws ArithmeticException
     * @throws InputMismatchException
     */
    public static int test() throws ArithmeticException,InputMismatchException {
        Scanner input=new Scanner(System.in);
        System.out.println("========运算开始==========");
        System.out.println("请输入第一个整数:");
        int one=input.nextInt();
        System.out.println("请输入第二个整数:");
        int two=input.nextInt();
        System.out.println("========运算结束==========");
        return one/two;
        
    }


}

六、throw抛出异常

1、通过try-catch包含throw语句,自己抛出自己处理

package com.imooc.exception;

import java.util.Scanner;

public class ThrowDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        testAge();
        
    }
    /**
     * 描述酒店入住规则:限定年龄,18岁以下,80岁以上的住客必须由亲友陪同
     * 1、throw自己抛出自己处理
     */
    public static void testAge() {
        try {
            System.out.println("请输入年龄:");
            Scanner input = new Scanner(System.in);
            int age=input.nextInt();
            if(age<18||age>80) {
                throw new Exception("18岁以下,80岁以上的住客必须由亲友陪同!");
            }else {
                System.out.println("欢迎入住酒店");
            }
            
        }catch(Exception e) {
            e.printStackTrace();
        }
        
        
    }
}

 2、通过throws在方法声明处抛出异常类型——谁调用谁处理——调用者可以自己处理,也可以继续往上抛

package com.imooc.exception;

import java.util.Scanner;

public class ThrowDemo2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            testAge();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    /**
     * 描述酒店入住规则:限定年龄,18岁以下,80岁以上的住客必须由亲友陪同
     * 1、通过try-catch包含throw语句,自己抛出自己处理
     * 2、通过throws在方法声明处抛出异常类型——谁调用谁处理——调用者可以自己处理,也可以继续往上抛
     * @throws Exception 
     */
    public static void testAge() throws Exception {
        
        System.out.println("请输入年龄:");
        Scanner input = new Scanner(System.in);
        int age=input.nextInt();
        if(age<18||age>80) {
            throw new Exception("18岁以下,80岁以上的住客必须由亲友陪同!");
        }else {
            System.out.println("欢迎入住酒店");
        }
            
        
        
        
    }
}

3、注意事项

1)throw建议抛出检查类型异常,不建议抛出非检查类型异常

2)throws声明的异常类型可以是throw抛出的异常同类型,或者父类型,不能是子类型

七、自定义异常

1、自定义异常类继承Exception

package com.imooc.exception;

//自定义异常类
public class HotelAgeException extends Exception {
    public HotelAgeException() {
        super("18岁以下,80岁以上的住客必须由亲友陪同!");
    }
}

2、抛出自定义类,并声明处理

package com.imooc.exception;

import java.util.Scanner;

public class ThrowDemo3 {

    public static void main(String[] args) {
        
        try {
            testAge();
        } catch (HotelAgeException e) {
            System.out.println(e.getMessage());            
            System.out.println("工作人员不允许办理入住!");
            e.printStackTrace();
        }
        
    }

    public static void testAge() throws HotelAgeException {
        
        System.out.println("请输入年龄:");
        Scanner input = new Scanner(System.in);
        int age=input.nextInt();
        if(age<18||age>80) {
            throw new HotelAgeException();
        }else {
            System.out.println("欢迎入住酒店");
        }
            
        
        
        
    }
}

八、异常链

package com.imooc.exception;

public class ThrowDemo4 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            testThree();
        } catch (Exception e) {    
            e.printStackTrace();
        }

    }
    public static void testOne() throws HotelAgeException {
        throw new HotelAgeException();
        
    }
    public static void testTwo() throws Exception {
        try {
            testOne();
        } catch (HotelAgeException e) {
            throw new Exception("我是新产生的异常1!",e);
        }
    }
    public static void testThree() throws Exception {
        try {
            testTwo();
        } catch (Exception e) {
            Exception e1=new Exception("我是新产生的异常2!");
            e1.initCause(e);
            throw e1;
//            throw new Exception();
        }
        
    }
}

猜你喜欢

转载自www.cnblogs.com/loveapple/p/11139016.html