面向对象(异常try-catch)

//copyright©liupengcheng
//http://www.cnblogs.com/liupengcheng

/*
* 异常的处理
* try
* {
*   需要被检测的代码
* }
* catch(异常类 变量)
* {
*   处理异常的代码(处理方式)
* }
* finally
* {
*   一定会执行的语句
* }
* */

//copyright©liupengcheng
//http://www.cnblogs.com/liupengcheng

class Demo3
{
    int div(int a,int b) throws Exception //声明函数有可能出现异常
    {
        return a/b;
    }
}

//copyright©liupengcheng
//http://www.cnblogs.com/liupengcheng


public class ExceptionDemo {
    public static void main(String [] args)
    {
        Demo3 d = new Demo3();
        try
        {
            int x = d.div(4,0);
            System.out.println("x"+ x);
        }
        catch(Exception e)
        {
            System.out.println("除零了");
            System.out.println(e.getMessage());
        }

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

//copyright©liupengcheng
//http://www.cnblogs.com/liupengcheng
posted @ 2014-09-28 15:20  liupengcheng  阅读(185)  评论(0编辑  收藏  举报