面向对象04异常

public class ExceptionTest {

    /**
     * 生活中的异常:---》摔跤
     *    下楼梯的时候,我也不想摔跤,但是确实摔了!
     *    然后呢??难道后半辈子就不过了??
     *    之后的生活肯定还得继续!!!
     *    
     * 程序中的异常 : 在程序运行期间,发生了不正常的事件(代码的问题),中断了程序执行!
     *               从出现异常的地方,之后的代码都不会执行!
     *               显然不符合我们的需求!
     *               我们的需求---》继续执行后续的代码!
     *               怎么执行后续的代码?
     * 使用异常处理机制:
     *      给程序提供了一种处理错误的能力! 出现异常之后,程序还能运行!  
     *      
     *            
     * int num = 5.0;  这行代码不能编译成功! 5.0是double类型 不能自动转换成int
     * Type mismatch: cannot convert from double to int
     * 这时候出现的问题,我们称之为 编译时异常!
     * 
     * 
     * 
     * 异常处理机制中:常用的五个关键字!
     * 1.try:
               01.把可能会出现异常的代码块   放在try中   程序就可以继续执行
               02.try不可以单独使用!必须和catch/finally 联合使用
               03.try代码块中声明的变量,仅限于本代码块中!
     * 2.catch:
     *        01.捕捉try代码块中出现的异常信息
     *        02.如果在catch的参数中写的是具体的某个异常
     *           那么catch块只能捕获这一个具体的异常
     *        03.如果想同时捕捉多个异常,我们可以书写多个catch代码块
     *        04.多个catch并存的时候!有书写顺序!  类型必须是 由小到大!
     *             父类异常只能放在最后!
     * 3.finally:
     *         01.无论如何都要执行的代码块    
     *         02.System.exit(0);  正常退出  
     *           如果参数是非0!代表异常退出!
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            System.out.println("请您输入第一个数字:");
            int num1 = scanner.nextInt();
            System.out.println("请您输入第二个数字:");
            int num2 = scanner.nextInt();
            /**
             *java.lang.ArithmeticException: / by zero  算术异常
             */
            System.out.println("两个数字的商是:" + (num1 / num2));
        } catch (ArithmeticException e) {
            System.out.println("除数不能为0!");
            System.exit(0); // 正常退出
        } catch (InputMismatchException e) {
            System.out.println("亲!输入格式有误!");
        } catch (Exception e) {
            e.printStackTrace(); // 打印出捕获到的异常信息
        } finally {
            System.out.println("这是finally代码块!");
        }
        System.out.println("程序结束");
    }
}
/**
 *学生的实体类
 *
 *4.throw
 *    01.抛出异常
 *    02.必须位于方法体内
 *    03.只能抛出一个异常
 *5.throws
 *   01.声明异常 出现在声明方法的后边(参数列表之后)
 *   02.可以有N个
 *   03.多个异常之间使用,隔开
 *   
 */
public class Student {

    private String name; // 姓名
    private String sex; // 性别
    private int age; // 年龄

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    /**
     * 如果年龄不在0-150之间  我们就抛出异常
     * @param age  用户传递过来的参数
     * @throws Exception  声明的异常
     *  01.声明的异常类 必须是抛出的异常类 或者其父类
     *  02.小例子:
     *     比如说,我们去买一辆自行车!
     *     本身整个车都有问题,然后卖家说 之后车轮有毛病!  不行!
     *     本身车轮有毛病! 卖家说车有毛病! 行!
     *     整个车以及车轮 我们理解成 抛出异常!
     *     卖家说的 都是 声明的异常!
     *     
     *   03.别人都给我们说有毛病了!
     *     然后我们必须去解决这个问题!  
     */
    public void setAge(int age) throws Exception {
        if (age < 0 || age > 150) {
            // 抛出异常
            throw new Exception("年龄不合法!");
        }
        this.age = age;
    }

    public Student(String name, String sex, int age) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public Student() {
        super();
    }

    // 测试方法
    public static void main(String[] args) {
        // 创建学生对象
        Student stu = new Student();
        // 给对象的 属性赋值
        try {
            stu.setAge(-100);// 不健康的输入
        } catch (Exception e) {
            e.printStackTrace();
        }
        /**
         * 对于我们调用的方法 有异常需要处理!
         * 两种方式:
         *   01.我们自己使用异常处理机制解决
         *   02.继续抛出异常,最后是交给了JVM来处理!
         */
    }

}
/**
 * 自定义异常类  ----》针对于Student
 * 01.继承RunTimeException
 * 02.继承Exception
 * 03.继承Throwable
 */
public class StudentException extends Exception {

    // 给用户一个提示信息
    public StudentException(String msg) {
        super(msg);
    }

    public StudentException() {
    }

}
/**
 *学生的实体类
 *
 */
public class Student {

    private String name; // 姓名
    private String sex; // 性别
    private int age; // 年龄

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    /**
     * 性别只能是男和女
     * @param sex  性别
     * @throws StudentException 
     */
    public void setSex(String sex) throws StudentException {
        if (sex.equals("男") || sex.equals("女")) {
            this.sex = sex;
        } else {
            throw new StudentException("性别输入异常!");
        }
    }

    public int getAge() {
        return age;
    }

    /**
     * 使用我们自定义的异常类  来处理
     * @throws StudentException 
     */
    public void setAge(int age) throws StudentException {
        if (age < 0 || age > 150) {
            throw new StudentException("年龄异常!");
        }
    }

    public Student(String name, String sex, int age) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public Student() {
        super();
    }

    // 测试方法
    public static void main(String[] args) {
        // 创建学生对象
        Student stu = new Student();
        try {
            stu.setAge(-90);
            stu.setSex("小人");
        } catch (StudentException e) {
            e.printStackTrace();
        }
    }
}
public class ExceptionTest {

    /**
     * 面试题:
     *   存在return的时候,finally的执行顺序!
     *   01.首先会执行finally中的代码块
     *   02.执行完毕之后再去执行return
     * 
     * 
     * 想把某段代码块进行 try  catch:快捷键
     * 01.选中需要捕获的代码块
     * 02.shift  +alt  +z
     * 
     */
    public static void main(String[] args) {

        try {
            System.out.println(111);
            System.out.println(222);
            System.out.println(333);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("finally代码块!");
        }
        System.out.println("程序结束");
    }

}
原文地址:https://www.cnblogs.com/xtdxs/p/7094073.html