Exception and Error

 

    Today I suddently be confused with the Exception and Error of Java. So I checked some infos about this. 

The Java Language Specification says that, The unchecked exceptions classes are the class RuntimeException and its

subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes. The Java API defines a number of exception classes, both checked and unchecked. Additional exception classes, both checked and unchecked, may be declared by programmers.

    The checked exception classes named in the throws clause are part of the contract between the implementor and user of the method or constructor. The throws clause of an overriding method may not specify that this method will result in throwing any checked exception which the overridden method is not permitted, by its throws clause, to throw. When interfaces are involved, more than one method declaration may be overridden by a single overriding declaration. In this case, the overriding declaration must have a throws clause that is compatible with all the overridden declarations.

    We often encounter is the Exception, not the Error. Both of them extends the class Throwable. The documentation says that the class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch. A little example referenced from the book Thinking in Java lies below.

-----------------------------------------------------

package exceptions;

 

class SimpleException extends Exception {

}

 

public class InheritingExceptions {

public void f() throws SimpleException {

System.out.println("Throw SimpleException from f()");

throw new SimpleException();

}

 

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

InheritingExceptions sed = new InheritingExceptions();

try {

sed.f();

} catch (SimpleException e) {

System.out.println("Caught it!");

}

System.out.println("the program continuse after the try bloch");

}

 

}

-----------------------------------------------------

The Error in Java program is the unchecked Exceptions. An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications   should not try to catch it. A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.

(some contents are referenced from Java Language Specification, jdk6.0 documentation, Thinking in java 4th)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值