java提供了两类主要的异常:runtime exception与checked exception.所有的checked exception是从java.lang.exception类衍生出来的,而runtime exception则是从java.lang.runtimeexception或java.lang.error类衍生出来的.
一.机制上 【程序编程相关:如何做好一个项目(转载)】它们的不同之处表现在两方面:机制上与逻辑上. 【推荐阅读:关于软件工程理论的认识与运用误区(转载)】
public class checkedexception extends exception 【扩展信息:中国与印度软件开发之比较(转载)】
它们在机制上的不同表现在两点:1.如何定义方法;2. 如何处理抛出的异常.请看下面checkedexception的定义:{ public checkedexception() {} public checkedexception( string message ) { super( message ); }} 以及一个使用exception的例子: public class exceptionalclass{ public void method1() throws checkedexception { // ... throw new checkedexception( "...出错了" ); } public void method2( string arg ) { if( arg == null ) { throw new nullpointerexception( "method2的参数arg是null!" ); } } public void method3() throws checkedexception { method1(); }} 你可能已经注意到了,两个方法method1()与method2()都会抛出exception,可是只有method1()做了声明.另外,method3()本身并不会抛出exception,可是它却声明会抛出checkedexception.... 下一页