首页 > 学院 > 开发设计 > 正文

面向对象(throws和throw的区别)

2019-11-14 21:14:57
字体:
来源:转载
供稿:网友
面向对象(throws和throw的区别)

/** * Created by Administrator on 2014/9/29. * * throws和throw的区别 * * throws使用在函数上,throw使用在函数内 * * throws后面跟的是异常类,可以跟多个,用逗号连接 * throw后面跟的是异常对象 */ //copyright?liupengcheng //http://www.VEVb.com/liupengcheng class fushuException extends Exception {     PRivate int value;     fushuException(String msg,int value)     {         super(msg);         this.value = value;     }     public int getValue()     {         return value;     } }

//copyright?liupengcheng //http://www.VEVb.com/liupengcheng

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

//copyright?liupengcheng //http://www.VEVb.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("出现问题的数值是"+ e.getValue());         }

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

//copyright?liupengcheng //http://www.VEVb.com/liupengcheng


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表