原文所在地址:http://fishermen.iteye.com/blog/24031javascript
理解error和exception之间的区别,学习如何对其进行处理 java
不少程序员不清楚error和exception之间的区别,这区别对于如何正确的处理问题而言很是重要(见附1,“简要的叙述error和exception”)。就像Mary Campione的“The Java Tutorial”中所写的:“exception就是在程序执行中所发生的中断了正常指令流的事件(An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.)。”依照美国传统辞典(American Heritage Dictionary)所解释的,error就是:“效果或状况背离了可接受的通常法则(The act or an instance of deviating from an accepted code of behavior.)”
背离(deviation)、中断(disruption),有什么区别呢?让咱们来这样想:若是你驱车在公路上行驶时有人拦住了你,这就叫作“中断”。若是车根本就没法发动,那就叫作“背离”。
这与Java有什么关系呢?不少。Java中有一个至关有趣的error和exception的结构。
是的,很是正确:全部使用try{} catch(Exception e){}的代码块只能找到你一半的错误。可是,是否try并catch Throwable取决于你捕捉它的缘由。快速的看一下Error的子类,它们的名字相似VirtualMachineError,ThreadDeath,LinkageError。当你想捕获这些家伙们的时候,你要肯定你须要捕获它们。由于那些都是很严重的错误。
可是ClassCastException是一个error吗?不彻底是。ClassCastException或任何类型的exception只是虚拟机(VM,VirtualMachine)让你知道有问题发生的方式,这说明,开发者产生了一个错误,如今有一个机会去修正它。
另外一方面,error是虚拟机的问题(一般是这样,但也多是操做系统的问题)。引用Java文档中关于error的说明:“Error是Throwable的子类,它的出现说明出现了严重的问题。通常应用程序除非有理由,不然不该该捕捉Error。一般这是很是反常的状况。”
因此,error很是强大,并且但处理它远比通常开发者想象的要难(固然不是你)。若是你在作一项很简单的工做的话,你认为有必要去处理error?
首先,记住,error跟exception抛出的方式大致相同的,只有一点不一样。就是一个抛出error的方法不须要对此进行声明(换句话说,这是一个unchecked exception(也被称作Runtime Exception))。
- public void myFirstMethod() throws Exception
-
- //Since it's an exception, I have to declare
-
- //it in the throws clause {
-
- throw new Exception();
-
- }
-
-
-
- public void mySecondMethod()
-
- //Because errors aren't supposed to occur, you
-
- //don't have to declare them.
-
- {
-
- throw new Error();
-
- }
注意,有一些exception是不可控制的(unchecked exception),跟error的表现是同样的,如:NullPointerException,ClassCastException和IndexOutOfBoundsException,它们都是RuntimeException的子类。RuntimeException和其子类都是unchecked excception。
那应该如何处理这些使人讨厌的unchecked exception呢?你能够在可能出现问题的地方catch它们,但这只是一个不完整的解决方法。这样虽然解决了一个问题,但其他的代码仍可能被其余unchecked exception所中断。这里有一个更好的办法,感谢ThreadGroup类提供了这个很棒的方法:
- public class ApplicationLoader extends ThreadGroup
-
- {
-
- private ApplicationLoader()
-
- {
-
- super("ApplicationLoader");
-
- }
-
-
-
- public static void main(String[] args)
-
- {
-
- Runnable appStarter = new Runnable()
-
- {
-
- public void run()
-
- {
-
- //invoke your application
-
- (i.e. MySystem.main(args)
-
- }
-
- }
-
- new Thread(new ApplicationLoader(), appStarter).start();
-
- }
-
-
-
- //We overload this method from our parent
-
- //ThreadGroup , which will make sure that it
-
- //gets called when it needs to be. This is
-
- //where the magic occurs.
-
- public void uncaughtException(Thread thread, Throwable exception)
-
- {
-
- //Handle the error/exception.
-
- //Typical operations might be displaying a
-
- //useful dialog, writing to an event log, etc.
-
- }
这个技巧太棒了。想一想这种状况,你对你的GUI程序进行了修改,而后一个unchecked exception被抛出了。而且你的GUI程序经常具备了错误的状态(对话框仍旧开着,按钮失效了,光标状态出现错误)。可是,使用这个技巧,你能够将你的GUI程序恢复原始状态并通知用户出现了错误。对本身感受很棒吧,由于你写了一个高质量的应用程序。
这个技巧并不仅适用于GUI程序。服务器端应用程序可使用这个技巧来释放资源,防止虚拟机进入不稳定状态。较早的捕获错误并聪明的将其处理是好的程序员和普通程序员的区别之一。你已经明白了这些,我知道你想成为哪一类程序员。
[color=cyan]关于做者
Josh Street是Bank of America的构架设计师。他主要负责电子商务平台的开发。他的联系方式是rjstreet@computer.org。
[/color]
附1
简要的叙述error和exception
Error和Exception都继承自Throwable,他们下列不一样处:
Exceptions
1.能够是 可被控制(checked) 或 不可控制的(unchecked)
2.表示一个由程序员致使的错误
3.应该在应用程序级被处理
Errors
1.老是 不可控制的(unchecked)
2.常常用来用于表示系统错误或低层资源的错误
3.如何可能的话,应该在系统级被捕捉