JAVA基础:异常处理

    * 异常处理
     * Throwable(异常处理的父类)
     * Error:       系统崩溃    数据库崩溃(跟代码没关系)
     * Exception:   异常(是跟代码有关系的)
     * RuntimeException:    运行时异常
     * 
     * 出现异常,系统如何解决(谁来解决)
     * main函数解决的方式解决的方式:
     * 1.main自己解决
     * 2.把问题抛给上级去解决(谁调用的main谁解决)
     *   交给JVM虚拟机去解决
     *   JVM虚拟机如何解决?给你报错
     *   就是打印错误信息,错误发生在什么类的什么位置 
     * 
     * 思考:如何解决异常

    * 解决异常方式
     * try...catch...finally
     * try      指测试这个遗产
     * catch        指捕获这个异常
     * finally  指异常结束后做的事
     *捕获异常的流程:
     *1.发生异常
     *2.根据发生的异常产生对应的异常对象
     *3.这个异常对象会返回给调用者
     *4.如果调用者处理了这个异常就是添加了try..catch
     *  异常对象会跟catch进行匹配,匹配上执行catch中的语句
     *  程序会继续执行
     *5.如果调用者没有处理这个异常,默认交给jvm去处理
     *  根据产生异常对象,打印对应需错误信息
     *  程序停止
     */
    public class Demo02 {
        public static void main(String[] args) {



        TestException test = new TestException();
        try {
            //可能发生异常的代码
            int num = test.fun(10,0);
            //这块放回了异常对象
            //new ArithmeticException("/by zero")
            System.out.println(num);
        }catch (ArithmeticException e) {
            // 捕获异常,该怎么办?
            //catch如何能捕获异常?
            // ArithmeticException e = new ArithmeticException("/by zero")  
            //catch会匹配这个异常对象,如果匹配上了就执行catch中的语句
            //捕获异常后,程序会继续执行
            System.out.println("吃药了没?除数为0了");


        }
        System.out.println("执行了吗?");
        }
    }

    class TestException{
        //异常方法
        public int fun(int a ,int b) {
            /*
             * 这里发生了算数异常
             * 相当于产生了一个异常对象
             * new ArithmeticException("/by zero")
             * 一旦发生异常系统会产生一个对应异常对象
             * 发生空指针
             * new NullPointerException();
             * 发生异常后,谁调用我这个异常就给谁
             * 处理不了,就给jvm虚拟机去处理
             * jvm默认处理方法:
             * 打印错误信息
             * (根据异常对象的类型,去打印错误信息)
             * 然后打印完会结束你的程序
             */
            return a/b;

        }
    }




     * 多catch的捕获异常
     * 异常处理的作用:帮助你来完善代码让出现的错误更容易找到
     * 
     * java
     * 安卓开发:客户端开发,发生异常的时候,一般直接用Exception接收就可以了
     * javaEE开发
     * 服务器开发,处理异常,打印错误信息,代码的错误会被隐藏掉.
     * 一般会做日志处理(记录程序被访问的日志)log4j
     * 
     */
    public class Demo03 {
        public static void main(String[] args) {
            int[] array = {1,2,3,4};

            try {
                //测试代码一场
                System.out.println(array[10/0]);
                //产生 new ArithmeticException,之后就去匹配
                //匹配成功后,程序继续向下执行
                System.out.println(array[10]);
            }catch(ArithmeticException e) {
                /*
                 * 注意:如果使用Exception直接匹配异常
                 * 多个catch同时捕获的时候,需要写在最后,
                 * 写前面,后面的catch相当于白写了
                 */
                System.out.println("病治了,除数为0了");
            }   catch (ArrayIndexOutOfBoundsException e) {
                //如果发生多个异常需要匹配多个catch
                System.out.println("别放弃,数组越界了");
            }   catch (Exception e) {
                System.out.println("你出错了");// TODO: handle exception
            }
            System.out.println("猜猜我执行了吗");
        }
    }



     * finally(遗言)
     * 记住:不管你异常有没有发生,有没有被匹配到都会执行
     * 
     * finally有什么作用?
     * 可以关闭系统资源,避免资源的浪费.
     * (例如:关闭输入流和关闭数据库)
     * 
     * 思考:
     * final  finally  findlize 有什么区别
     * 雷锋和雷峰塔的区别,没什么联系.
     * final 修饰类 修饰方法 修饰属性
     * finally 中的语句一定会执行(异常处理的语句)
     * finalize 基类object中的方法,所有java中的对象都有这个方法
     * 该方法在垃圾回收时候被系统自动调用
     * 当一块内从空间无人引用,这块内从就是垃圾
     */
    public class Demo04 {
        public static void main(String[] args) {
            try {
                System.out.println(10/1);
                return;
            } catch (ArithmeticException e) {
                System.out.println("除0了");// TODO: handle exception
            }finally {
                System.out.println("你看我就行了吗?");
            }
            System.out.println("我是下面的语句");
        }
    }


    * 运行时异常和编译时异常
     * 除了运行时异常;就是编译时异常
     * 出现运行时异常----是代码问题(程序员犯的错误)
     * 出现编译时异常----问题的提前预警,强制你去处理
     *                不处理编译不通过
     * 
     */
    public class Demo05 {
        public static void main(String[] args) throws FileNotFoundException {
            //编译时异常
            //系统这时候不知道到底有没有这个文件
            //相当于系统问你,要是没有这个文件该如何进行
            //系统这时会强制要求你对这个问题做出解释
            //这就是编译时异常(相当于对问题的一种提前准备)

            /*
             * 解决异常有两种方式
             * 1.自己处理try..catch
             *  (想让程序继续执行就try)
             * 2.把异常抛出去(谁调用我,抛给谁,谁去处理)
             *  (想让程序遇到错误就停止就抛)
             * 
             */
            /*
            try {

                FileInputStream fileInputStream = new FileInputStream("wlxxxx.txt");

            } catch (Exception e) {

                System.out.println("读取文件没找到");

            }
            */
            FileInputStream fileInputStream = new FileInputStream("wlxxxx.txt");

        }
    }

    * 需求创建一个人类有name和age
     * 要求:人类的年龄赋值时,要在0到120岁之前
     * 不用异常
     */
    public class Demo06 {
        public static void main(String[] args) throws Exception {
            Person person = new Person();
            //赋值年龄
            person.setAge(-16);

            System.out.println(person.getAge());

    //      System.out.println("请输入你的年龄");
    //      Scanner scanner = new Scanner(System.in);
    //      
    //      if () {
    //          
    //      }
    //      
    //      String string = scanner.nextLine();

        }
    }

    //创建一个自定义的异常类(继承Exception)
    //在创建异常类时,需要直观让人知道这是什么异常
    //所以类名要见名知意 
    class AgeOutOfBoudsException extends Exception{
        /**
         * 序列化时需要Id
         */
        //重写构造方法
        public AgeOutOfBoudsException() {
            super();
        }
        public AgeOutOfBoudsException(String message) {
            super(message);

        }       
    }


    * Throwable中的方法
     * 
     */
    public class Demo07 {
        public static void main(String[] args) {

            Exception e = new Exception("我是异常信息");
            //调用异常类中的toString方法
            //打印的异常的类名和异常信息
            //System.out.println(e);
            //只打印异常信息
            //System.out.println(e.getMessage());
            //打印的是异常类名信息加出现的位置
            //jvm虚拟机默认就是打印这个方法
            e.printStackTrace();
        }
    }

    public class Demo08 {
        public static void main(String[] args) {
            //无限输入整数,存放在集合中,打印输入quit停止
            //希望在输入字符串的时候让程序也能继续运行.
            System.out.println("请输入整数");
            Scanner scanner = new Scanner(System.in);
            ArrayList<Integer> list = new ArrayList<>();
            while (true) {
                //接收输入的字符串
                String string = scanner.nextLine();
                //判断
                if (string.equals("quit")) {
                    //break跳出本层循环
                    //return结束这个方法
                    break;
                }

                //自己处理这个异常
                try {
                    //字符串转数字
                    int num = Integer.parseInt(string);
                    //添加进数组中
                    list.add(num);
                } catch (NumberFormatException e) {
                    System.out.println("输入错误,请重新输入或者quit");             
                }

            }
            //遍历集合打印
            for (Integer integer : list) {
                System.out.println(integer);
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值