面向对象(自定义异常)

/**
* Created by Administrator on 2014/9/29.
*
* 自定义异常,需要继承Exception.
*/

//copyright©liupengcheng
//
http://www.cnblogs.com/liupengcheng
class fushuException extends Exception
{
    fushuException(String msg)
    {
        super(msg);
    }
}

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

class Demo5
{
    int div(int a,int b)    throws fushuException //fushuException    自定义负数异常
    {
        if(b<0)
            throw new fushuException("出现了除数是负数的情况");
        return a/b;
    }
}

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


public class ExceptionDemo3 {
    public static void main(String [] args)
    {
        Demo5 d = new Demo5();
        try
        {
            int x = d.div(4,-1);
            System.out.println("x"+ x);
        }
        catch(fushuException e)
        {
            System.out.println(e.toString());
        }

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

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

原文地址:https://www.cnblogs.com/liupengcheng/p/4000167.html