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

王朝学院·作者佚名  2016-05-29
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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

/** * Created by Administrator on 2014/9/29. * * throws和throw的区别 * * throws使用在函数上,throw使用在函数内 * * throws后面跟的是异常类,可以跟多个,用逗号连接 * throw后面跟的是异常对象 */ //copyright?liupengcheng //http://www.cnblogs.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.cnblogs.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.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("出现问题的数值是"+ e.getValue());         }

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

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

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航