Java_基础—try...catch的方式处理异常1

package com.soar.exception;

public class Demo2_Exception {
    /*
     * * A:异常处理的两种方式
        * a:trycatchfinally的三种搭配
        * ① try catch
        * ② try catch finally
        * ③ try finally 
        * b:throws
       * B:try...catch处理异常的基本格式
        * trycatchfinally
      * C:案例演示
        * try...catch的方式处理1个异常

        try:用来检测异常
        catch:用来捕获异常
        finally:释放资源

        世界上最真情的相依,就是你在try,我在catch,无论你发神马脾气,我都静静接受,默默处理
        在try中出现了问题,再找catch,如果没有问题,就不找catch
        当通过try-catch将问题处理了,程序会继续执行。
     */
    public static void main(String[] args) {
        Demo_div d = new Demo_div();
        try{
            int x = d.div(10, 0);   
            System.out.println(x);
        }catch(ArithmeticException a){      //ArithmeticException a = new ArithmeticException();  
            System.out.println("出错了,除数为0啦");    //出错了,除数为0啦
        }       
        System.out.println("这句话会被执行吗?");        //这句话会被执行吗?
    }
}
class Demo_div{
    //除法运算
    public int div(int a, int b){   //a=10, b=0
        return a/b;                 //10/0 被除数是10,除数是0,当除数是0的时候,违背了算数运算法则,抛出异常
                                    //new ArithmeticException("/ by zero");
    }
}
posted @ 2017-07-20 09:35  Soar_Sir  阅读(117)  评论(0编辑  收藏  举报