java-异常

  1. 异常简介

    • 有异与常态,和正常情况不一样,有错误出现,阻止当前方法或作用域,称之为异常。
  2. 处理异常的意义

    • 将异常提示给编程人员或者用户,使本来中断的程序以适当的方式继运行或退出并且保存用户的当前操作或数据回滚最后把占用的资源释放掉。
  3. 异常种类

    • Throwable:java中所有的异常都继承它,java.lang.Throwable,其中又 分为Error和Exception。
    • Error是错误,java.lang.Error
    • Exception是异常,java.lang.Exception

又分类如图:
这里写图片描述
这里写图片描述

4.处理异常

try-catch或try-catch-finally

    try{
        //一些会抛出异常的方法
    }catch(Exception e){
        //处理异常的代码块              
    }catch(Exception2 e){
        //处理异常的代码块              
    }...{
    }finally{
        //最终将要执行的代码块
    }

5.实例
例1

        public static void main(String[] args) {
        // TODO Auto-generated method stub
        try{
        System.out.println("请输入你今年的年龄:");
        Scanner input = new Scanner(System.in);
        int age = input.nextInt();
        System.out.println("十年后你的年龄为:"+(age+10)+"岁!");
        }catch(InputMismatchException e){
            System.out.println("请输入整数!");
        }finally{
            System.out.println("程序结束了!");
        }

    }

例2

package 异常;

public class TryCatchTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TryCatchTest tct = new TryCatchTest();
        int result = tct.test();
        System.out.println("test方法,执行完毕,返回值为:"+result);
        int result2 = tct.test2();
        System.out.println("test2方法,执行完毕,返回值为:"+result2);
    }
    /**
     * 定义两个变量divider和result,用try-catch捕获while循环,每次循环divider减一,
     * result = result + 100/divider;如果捕获异常就打印出"抛出异常了"返回-1
     * 否则返回result
     */
    public int test(){
        int divider = 10,result = 100;
        try{
            while(divider>-1){
                divider--;
                result = result + 100/divider;          
            }
         return result;
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("循环抛出异常了!!");
            return -1;
        }       
    }
    /**
     * 定义两个变量divider和result,用try-catch捕获while循环,每次循环divider减一,
     * result = result + 100/divider;如果捕获异常就打印出"抛出异常了"返回999
     * 否则返回result, 
     * finally:打印输出“这是finally”同时打印出result的值。 
     */
    public int test2(){
        int divider = 10,result = 100;
        try{
            while(divider>-1){
                divider--;
                result = result + 100/divider;          
            }
         return result;
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("循环抛出异常了!!");
            return 999;
        }finally{
            System.out.println("这是finally,");
            System.out.println("这是result,result的结果为:"+result);
        }

    }

}

6.抛出异常

throw-声明将要抛出何种类型的异常(声明)

    public void 方法名 (参数列表)throws 异常列表{
        //调用会抛出异常方法或者:
        throw new Exception;
    }

这里写图片描述

7.自定义异常

class 自定义异常类 extends 异常类型{

}

8.代码

自定义异常:

public class DrunkException extends Exception {

    public DrunkException(String message){//含参构造器
        super(message);
    }

}

测试类

package 异常;

public class Test1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Test1 ts = new Test1();//创建一个实例
        try{
            ts.test2();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public void test() throws DrunkException{//出现异常的方法

        throw new DrunkException("和车别开酒!");

    }
    public void test2(){
        try{                                //捕获异常
            test();                         //调用异常的方法
        }catch(DrunkException e){
            RuntimeException rte = new RuntimeException("司机一滴酒,亲人两行泪!");
            rte.initCause(e);
            throw rte;
        }
    }
}
posted @ 2016-12-23 18:26  滕鹏飞  阅读(91)  评论(0编辑  收藏  举报