您的位置:首页 > 编程语言 > Java开发

Exceptions in Java and C#

2005-02-22 20:59 495 查看
头绪太多,整理中...
在Java中,Exception分为checked, unchecked两类。对于checked exception,如果一个方法在内部抛出了checked exception,那就必须在方法签名中申明它;而调用这个方法的其他方法,要么必须处理这个checked xception,要么就要重新申明并抛出同样的exception。而对于unchecked exception来说,则不需要去声明它。

在C#中,所有的exception都是不需要专门去声明的,就好像都是unchecked exception。

对于使用check or uncheck exception的问题上,Sun在他们的教程上是这样说的:
"Because the Java language does not require methods to catch or specify runtime exceptions, it's tempting for programmers to write code that throws only runtime exceptions or to make all of their exception subclasses inherit from RuntimeException. Both of these programming shortcuts allow programmers to write Java code without bothering with all of the nagging errors from the compiler and without bothering to specify or catch any exceptions. While this may seem convenient to the
programmer, it sidesteps the intent of Java's catch or specify requirement and can cause problems for the programmers using your classes. Checked exceptions represent useful information about the operation of a legally specified request that the caller may have had no control over and that the caller needs to be informed about -- for example, the file system is now full, or the remote end has closed the connection, or the access privileges don't allow this action. What does it buy you if you throw a RuntimeException or create a subclass of RuntimeException just because you don't want to deal with specifying it? Simply, you get the ability to throw an exception without specifying that you do so. In other words, it is a way to avoid documenting the exceptions that a method can throw. When is this good? Well, when is it ever good to avoid documenting a method's behavior? The answer is hardly ever. "

这些规则曾经是被广泛接受的,但是现在有很多人对此提出了反对意见,认为应该更广泛的使用unchecked exception。他们的理由是:
1. checked exception经常不适当的暴露方法的实现细节。比如说有一个方法是查询用户信息,当它找不到一个特定用户时,却经常抛出SQLException或是IOException,而不是一个具有特殊意义的NoSuchUserException。在Effective Java一书中,43条是这样说的: "Throw exceptions appropriate to the abstraction,In other words, exceptions thrown by a method should be defined at an abstraction level consistent with what the method does, not necessarily with the low-level details of how it is implemented. For example, a method that loads resources from files, databases, or JNDI should throw some sort of ResourceNotFound exception when it cannot find a resource (generally using exception chaining to preserve the underlying cause), rather than the lower-level IOException, SQLException, or NamingException." 所以,抛出一个有意义的exception,再将表明具体底层问题的加入到exception栈中以供调试是比较好的一个方法。
2. checked exception会导致方法申明的不稳定。这是一个很明显的问题,但是这个问题也可用43条来解决,将一个方法的exception抽象到一定的层次将会很有效的解决这种问题。
3. checked exception导致代码的可读性下降。试想一下如果一个只有两行代码,却不得不处理5、6个exception会是一种什么样的情形。
4. 第三条又会知道导致一些程序员直接用一条Catch(Exception ex)语句来处理所有的不同exceptions。

而使用unchecked exception最大的问题是如果文档跟不上的话,对于使用这些方法的人是个很大的麻烦,根本不知道这个方法会抛出一些什么异常,不好处理。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: