OP13newException.pptVIP

  • 12
  • 0
  • 约1.33万字
  • 约 43页
  • 2016-11-29 发布于河南
  • 举报
OP13newException

5、抛出例外(throw语句) 声明例外首先必须生成例外。前面所提到的例外或者是由Java虚拟机生成,或者是由Java类库中的某些类生成。事实上,我们在程序中也可以生成自己的例外对象,也即是例外可以不是出错产生,而是人为地抛出。 不论那种方式,生成例外对象都是通过throw语句实现: throw new ThrowableObject(); ArithmeticException e = new ArithmeticException(); throw e; 注意:抛出的例外必须是Throwable或其子类的实例。 A method Exception Another method class JavaThrow { public static void main(String args[]) { } } try { throw new ArithmeticException(); }catch(ArithmeticException ae){ System.out.println(ae); } try { throw new ArrayIndexOutOfBoundsException(); }catch(ArrayIndexOutOfBoundsException

文档评论(0)

1亿VIP精品文档

相关文档