黑马程序员——Java基础---自定义异常类

来源:互联网 发布:传奇霸业珠子数据 编辑:程序博客网 时间:2024/05/18 10:48
<pre name="code" class="java"><span style="line-height: 26px; font-size: 14px; font-family: SimSun;">-----------<a target=_blank target="_blank" href="http://www.itheima.com/" style="color: rgb(202, 0, 0); text-decoration: none;">android</a></span><a target=_blank target="_blank" href="http://www.itheima.com/" style="text-align: -webkit-center; text-decoration: none; line-height: 26px; font-size: 14px; font-family: SimSun; color: rgb(202, 0, 0);">培训</a><span style="text-align: center; text-indent: 28px; line-height: 26px; font-size: 14px; font-family: SimSun;">、</span><a target=_blank target="_blank" href="http://www.itheima.com/" rel="nofollow" style="text-align: center; text-indent: 28px; text-decoration: none; line-height: 26px; font-size: 14px; font-family: SimSun; color: rgb(202, 0, 0);">java培训</a><span style="text-align: center; text-indent: 28px; line-height: 26px; font-size: 14px; font-family: SimSun;">、java学习型技术博客、期待与您交流!------------</span>
<span style="text-align: center; text-indent: 28px; line-height: 26px; font-size: 14px; font-family: SimSun;"><span style="color:#ff0000;">1、需求</span></span>
<span style="text-align: center; text-indent: 28px; line-height: 26px; font-size: 14px; font-family: SimSun;">因为项目中会出现特有的问题,而这些问题并未被java所描述并封装对象。所以对于这些特有的问题可以按照java的对问题封装的思想。将特有的问题。进行自定义的异常封装。</span>
<span style="text-align: center; text-indent: 28px; line-height: 26px; font-size: 14px; font-family: SimSun;"></span>
<span style="text-align: center; text-indent: 28px; line-height: 26px; font-size: 14px; font-family: SimSun;"><span style="color:#ff0000;">2、自定义异常类方法</span></span>
<span style="font-size: 14px; line-height: 26px; font-family: SimSun; text-align: center; text-indent: 28px;">a)定义一个类,继承异常类体系中的任何一员即可,例如继承Exception。因为只有异常体系中的类才可以被throw和throws操作。</span>
<span style="font-size: 14px; line-height: 26px; font-family: SimSun; text-align: center; text-indent: 28px;">b)在自定义异常类中定义有参数的异常类构造函数。用来处理自定义异常信息。</span>
<span style="font-size: 14px; line-height: 26px; font-family: SimSun; text-align: center; text-indent: 28px;">c)在可能出现异常的方法中使用throw new myException(msg);语句抛出自定义异常信息。</span>
<span style="font-size: 14px; line-height: 26px; font-family: SimSun; text-align: center; text-indent: 28px;">d)在抛出异常信息的方法名上,必须使用throws声明抛出的异常。</span>
<span style="font-size: 14px; line-height: 26px; font-family: SimSun; text-align: center; text-indent: 28px;">e)在调用含抛出异常的方法的代码块上,使用try..catch..语句捕获异常。</span>
<span style="font-size: 14px; line-height: 26px; font-family: SimSun; text-align: center; text-indent: 28px;"></span>
3、throw和throws的区别。
throws使用在函数上,throw使用在函数内。
throws后面跟的异常类,可以跟多个,用逗号隔开;throw后跟的是异常对象。
<span style="font-family: Arial, Helvetica, sans-serif;"></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="color:#ff0000;">4、事例。</span></span>
//要想自定义一个异常类,必须继承异常体系中的类//比如Throwable、Error、Exceptionclass  FuShuException extends Exception{private int value;//构造函数FuShuException(){//调用父类构造函数super();}//构造函数FuShuException(String msg,int value){//调用父类构造函数super(msg);//处理本异常类中特有的数据this.value = value;}public int getValue(){return value;}}class ZeroException extends Exception{ZeroException(){super();}ZeroException(String msg){super(msg);}}class Demo{//如果函数体中,定义了throw语句,那么在方法名后必须声明throws//throws抛出的异常可以是函数体内出现的异常类或其父类int div(int a, int b)throws FuShuException,ZeroException,Exception{//可以只声明Exception,因为FuShuException,ZeroException都继承自Exception//int div(int a, int b)throws Exception{if(b<0){//自定义异常信息throw new FuShuException("除数不能为负!---/by fushu",b);}else if(b==0){//调用java异常体制中,Exception类的构造函数throw new ZeroException("自定义异常信息:除数不能为0。");}else if(a<5){//调用java异常体制中,Exception类的构造函数throw new Exception("默认Exception:a不能小于5");}return a/b;}}class ExcetionDemo3{public static void main(String[] args) {Demo d = new Demo();try{int x = d.div(4,-9);System.out.println("x="+x);//抛出自定义异常,只有异常系类的类才可以抛出}catch(FuShuException e){//调用自定义异常类中,继承自父类中的toSting方法。System.out.println(e.toString());//调用自定义异常类中特有的处理方法。System.out.print("错误的除数:"+e.getValue());}catch(ZeroException e){System.out.println(e.toString());}catch(Exception e){System.out.println(e.toString());}System.out.println("over");}}


5、运行结果。



0 0