简体   繁体   English

已检查与未检查异常统计

[英]checked vs unchecked exception statistics

After getting tired of convincing my peers to abandon checked exception in the argumentative way, I am looking for any statistics that will say how many programmers prefer checked exception, and how many prefer not to use them. 在厌倦了说服同事以争论的方式放弃检查异常之后,我正在寻找任何统计数据,这些统计数据将说明有多少程序员喜欢检查异常,以及有多少程序员不喜欢使用检查异常。
Is there such a survey / voting / statistics on the web? 网络上是否有这样的调查/投票/统计数据?

How about pointing them to the Java Tutorial: 如何将它们指向Java教程:

http://download.oracle.com/javase/tutorial/essential/exceptions/runtime.html http://download.oracle.com/javase/tutorial/essential/exceptions/runtime.html

which includes a final line which summarizes the key point clearly: 其中包括最后一行,清楚地总结了关键点:

Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. 这是底线准则:如果可以合理地预期客户端将从异常中恢复,则将其设置为已检查的异常。 If a client cannot do anything to recover from the exception, make it an unchecked exception. 如果客户端无法采取任何措施来从异常中恢复,请将其设置为未经检查的异常。

One problem with many languages and frameworks which derive their general exception-handling concepts from C++ is that way too many things are effectively encoded in the type of an exception. 从C ++派生其通用异常处理概念的许多语言和框架存在的一个问题是,太多的事情被有效地编码为异常类型。 Checked exceptions would be a good concept, if the checked-versus-unchecked distinction were made at the throw- and catch sites, rather than in the declaration of the exception , and if there were a way to concisely specify that particular checked exceptions should get rethrown as unchecked. 如果在抛出和捕获位置而不是在异常声明中进行了检查与未检查的区分 ,并且有一种方法可以简洁地指定应该使用特定的检查异常, 那么检查异常将是一个好概念重新抛出为未经检查。 Unfortunately, Java requires that any exception of a given type always be thrown as checked, or always as unchecked, depending upon the type, and provides no convenient or concise way to map checked exceptions to unchecked ones. 不幸的是,Java要求给定类型的任何异常都必须始终按检查类型抛出,或始终按未检查类型抛出,并且取决于类型,并且没有提供方便或简洁的方式来将已检查异常映射到未检查异常。 Instead, it makes it most convenient to have checked exceptions percolate up as checked exceptions, which increases the "nuisance" factor while reducing their usefulness. 取而代之的是,它使检查异常作为检查异常渗入最方便,这增加了“麻烦”因素,同时降低了其有用性。

If checked exceptions were implemented at the throw site, and if the normal way of handing unwanted checked exceptions was to convert them to unchecked, then one could be assured that a method which indicates a particular fault condition by throwing a particular checked exception would only throw that checked exception when it "wanted" to report that particular fault; 如果在抛出站点上实现了检查异常,并且处理不想要的检查异常的正常方法是将其转换为未检查异常,则可以确保通过抛出特定检查异常来指示特定故障条件的方法只会抛出在“希望”报告特定故障时检查异常; if the same type of exception occurred, unanticipated, during the execution of a method, it would get converted to an unchecked exception. 如果在方法执行期间发生了相同类型的异常,这是无法预料的,​​则它将转换为未经检查的异常。 Such a "checked exceptions" model could be useful, and could also lead to more efficient code than normal exceptions (since checked exceptions would have to be handled in the immediate caller of the routine throwing them, much of the stack unwinding and stack tracing required for normal exception handling could be skipped). 这样的“检查异常”模型可能有用,并且还可以导致比普通异常更有效的代码(因为检查异常必须在引发它们的例程的直接调用程序中进行处理,因此需要进行大量堆栈展开和堆栈跟踪)可以跳过正常的异常处理)。 Unfortunately, Java's "checked exceptions" don't work that way. 不幸的是,Java的“检查异常”不能那样工作。

If there was a survey like this wouldn't your cow-orkers just argue there are more bad programmers than good programmers on the web, and they would expect more bad programmers voted for unchecked exceptions? 如果进行这样的调查,那么您的牛郎们是否会认为网络上的不良程序员比不良程序员更多,并且他们希望更多不良程序员投票赞成未经检查的异常? So it doesn't really solve your problem. 因此,它并不能真正解决您的问题。

Two data points are convincing to me: 两个数据点使我信服:

  • No other language implementer anywhere afaik has copied checked exceptions from Java. afaik在任何地方都没有其他语言实现者从Java复制过检查的异常。 It is a total flop as far as catching on in other languages. 就其他语言而言,这是一次彻底的失败。 If there was anything good about the feature somebody would have appropriated it for their new language. 如果该功能有什么好处,那么有人会将该功能用于他们的新语言。

  • There has been a broad movement among framework writers, from Spring to Hibernate to Java EE, to replace many checked exceptions with unchecked ones. 从Spring到Hibernate再到Java EE,框架编写者之间发生了广泛的变化,用非检查异常代替许多检查异常。

Besides that you could try reviewing your projects' code and finding examples of how checked exceptions lead to problems with exceptions getting eaten or useless "throws Exception" clauses everywhere. 除此之外,您还可以尝试查看项目的代码,并找到有关检查异常如何导致异常被吞噬或无处不在的“引发异常”子句的问题的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM