异常

/*
* 异常:程序出现了不正常的情况。
*
* 举例:今天天气很好,班长出去旅游。骑着自行车,去山里面呼吸新鲜空气。
* 问题1:山路塌陷了,班长及时停住了,但是过不去了。严重的问题。
* 问题2:班长出门推自行车,发现气没了,把气吹起来。出发前就应该检查的问题。
* 问题3:班长骑着车在山路上惬意的行驶着,山路两边是有小石子的,中间是平坦的水泥路。
* 一直在平坦的水泥路上行驶是没有任何问题的,但是呢,他偏偏喜欢骑到小石子上,结果爆胎了。旅游的过程中出现的问题。
* no zuo no die。
*
* 程序的异常:Throwable
* 严重问题:Error 我们不处理。这种问题一般都是很严重的,比如说内存溢出。
* 问题:Exception
* 编译期问题:不是RuntimeException的异常 必须进行处理的,因为你不处理,编译就不能通过。
* 运行期问题:RuntimeException 这种问题我们也不处理,因为是你的问题,而且这个问题出现肯定是我们的代码不够严谨,需要修正代码的。
*
* 如何程序出现了问题,我们没有做任何处理,最终jvm会做出默认的处理。
* 把异常的名称,原因及出现的问题等信息输出在控制台。
* 同时会结束程序。
*/

public class ExceptionDemo {
public static void main(String[] args) {
//第一阶段
int a = 10;
// int b = 2;
int b = 0;
System.out.println(a / b);

//第二阶段
System.out.println("over");
}
}
package cn.itcast_02;

/*
 * 我们自己如何处理异常呢?
 * A:try...catch...finally
 * B:throws 抛出
 * 
 * try...catch...finally的处理格式:
 *         try {
 *             可能出现问题的代码;
 *         }catch(异常名 变量) {
 *             针对问题的处理;
 *         }finally {
 *             释放资源;
 *         }
 * 
 * 变形格式:
 *         try {
 *             可能出现问题的代码;
 *         }catch(异常名 变量) {
 *             针对问题的处理;
 *         }
 * 
 * 注意:
 *         A:try里面的代码越少越好
 *         B:catch里面必须有内容,哪怕是给出一个简单的提示
 */
public class ExceptionDemo {
    public static void main(String[] args) {
        // 第一阶段
        int a = 10;
        // int b = 2;
        int b = 0;

        try {
            System.out.println(a / b);
        } catch (ArithmeticException ae) {
            System.out.println("除数不能为0");
        }

        // 第二阶段
        System.out.println("over");
    }
}

package cn.itcast_02;

/*
 * A:一个异常
 * B:二个异常的处理
 *         a:每一个写一个try...catch
 *         b:写一个try,多个catch
 *             try{
 *                 ...
 *             }catch(异常类名 变量名) {
 *                 ...
 *             }
 *             catch(异常类名 变量名) {
 *                 ...
 *             }
 *             ...
 * 
 *             注意事项:
 *                 1:能明确的尽量明确,不要用大的来处理。
 *                 2:平级关系的异常谁前谁后无所谓,如果出现了子父关系,父必须在后面。
 * 
 * 注意:
 *         一旦try里面出了问题,就会在这里把问题给抛出去,然后和catch里面的问题进行匹配,
 *         一旦有匹配的,就执行catch里面的处理,然后结束了try...catch
 *         继续执行后面的语句。
 */
public class ExceptionDemo2 {
    public static void main(String[] args) {
        // method1();

        // method2();

        // method3();

        method4();
    }

    public static void method4() {
        int a = 10;
        int b = 0;
        int[] arr = { 1, 2, 3 };

        // 爷爷在最后
        try {
            System.out.println(a / b);
            System.out.println(arr[3]);
            System.out.println("这里出现了一个异常,你不太清楚是谁,该怎么办呢?");
        } catch (ArithmeticException e) {
            System.out.println("除数不能为0");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("你访问了不该的访问的索引");
        } catch (Exception e) {
            System.out.println("出问题了");
        }

        // 爷爷在前面是不可以的
        // try {
        // System.out.println(a / b);
        // System.out.println(arr[3]);
        // System.out.println("这里出现了一个异常,你不太清楚是谁,该怎么办呢?");
        // } catch (Exception e) {
        // System.out.println("出问题了");
        // } catch (ArithmeticException e) {
        // System.out.println("除数不能为0");
        // } catch (ArrayIndexOutOfBoundsException e) {
        // System.out.println("你访问了不该的访问的索引");
        // }

        System.out.println("over");
    }

    // 两个异常的处理
    public static void method3() {
        int a = 10;
        int b = 0;
        int[] arr = { 1, 2, 3 };

        try {
            System.out.println(arr[3]);
            System.out.println(a / b);
            // System.out.println(arr[3]);
        } catch (ArithmeticException e) {
            System.out.println("除数不能为0");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("你访问了不该的访问的索引");
        }

        System.out.println("over");
    }

    // 两个异常
    public static void method2() {
        int a = 10;
        int b = 0;
        try {
            System.out.println(a / b);
        } catch (ArithmeticException e) {
            System.out.println("除数不能为0");
        }

        int[] arr = { 1, 2, 3 };
        try {
            System.out.println(arr[3]);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("你访问了不该的访问的索引");
        }

        System.out.println("over");
    }

    // 一个异常
    public static void method1() {
        // 第一阶段
        int a = 10;
        // int b = 2;
        int b = 0;

        try {
            System.out.println(a / b);
        } catch (ArithmeticException ae) {
            System.out.println("除数不能为0");
        }

        // 第二阶段
        System.out.println("over");
    }
}
package cn.itcast_02;

/*
 * JDK7出现了一个新的异常处理方案:
 *         try{
 * 
 *         }catch(异常名1 | 异常名2 | ...  变量 ) {
 *             ...
 *         }
 * 
 *         注意:这个方法虽然简洁,但是也不够好。
 *             A:处理方式是一致的。(实际开发中,好多时候可能就是针对同类型的问题,给出同一个处理)
 *            B:多个异常间必须是平级关系。
 */
public class ExceptionDemo3 {
    public static void main(String[] args) {
        method();
    }

    public static void method() {
        int a = 10;
        int b = 0;
        int[] arr = { 1, 2, 3 };

        // try {
        // System.out.println(a / b);
        // System.out.println(arr[3]);
        // System.out.println("这里出现了一个异常,你不太清楚是谁,该怎么办呢?");
        // } catch (ArithmeticException e) {
        // System.out.println("除数不能为0");
        // } catch (ArrayIndexOutOfBoundsException e) {
        // System.out.println("你访问了不该的访问的索引");
        // } catch (Exception e) {
        // System.out.println("出问题了");
        // }

        // JDK7的处理方案
        try {
            System.out.println(a / b);
            System.out.println(arr[3]);
        } catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
            System.out.println("出问题了");
        }

        System.out.println("over");
    }

}

 

 

 

posted @ 2016-04-07 16:23  爪哇见习  阅读(186)  评论(0编辑  收藏  举报