什么是签入的Java / C#异常? [英] What are checked exceptions in Java/C#?

查看:208
本文介绍了什么是签入的Java / C#异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个C#开发者用Java做偶尔的编码。简单来说有人能解释什么检查了Java异常,以及为什么需要它?在C#中还没有碰到过这个词。

I am a C# developer doing occasional coding in Java. Can someone explain in simple terms what are checked exceptions in Java and why is it needed? Haven't come across this term in C#.

推荐答案

经过例外是编译器要求你以某种方式来处理。异常

Checked exceptions are exceptions that the compiler require you handle in some way.

在Java中,检查的例外是 的Throwable S中的不<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/lang/RuntimeException.html\"><$c$c>RuntimeException, 错误 ,或它们的一个子类。

In Java, checked exceptions are Throwables that are not RuntimeException, Error, or one of their subclasses.

Java的设计者认为,以保证程序处理的异常那名非常有可能需要它们。一个典型的例子是<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/io/IOException.html\"><$c$c>IOException.任何时候,一个程序执行I / O,有失败的可能性。磁盘空间已满,该文件可能不存在,有可能是一个权限问题等。

The Java designers felt they were needed to ensure programs handled exceptions that were reasonably likely. A classic example is IOException. Any time a program does I/O, there is a possibility of failure. The disk could be full, the file might not exist, there might be a permissions problem, etc.

因此​​,Java设计使得一个程序必须语法处理以某种方式除外。这可能是一个catch块,或以某种方式重新抛出异常。

Thus, Java is designed such that a program must syntactically handle the exception in some way. This could be with a catch block, or by rethrowing the exception in some way.

C#没有检查异常。他们决定离开这个问题到应用开发者(采访)。 checked异常是有争议的,因为它们可以让code冗长,而开发人员有时与空catch块平凡的处理。此外,它可以是任意哪个标准库方法抛出检查异常。例如,为什么不<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/io/File.html#delete%28%29\"><$c$c>File.delete (一个新的Java API 7与此不同)抛出 IOException异常

C# does not have checked exceptions. They decided to leave this issue up to the application developers (interview). Checked exceptions are controversial because they can make code verbose, while developers sometimes handle them trivially with empty catch blocks. Further, it can be arbitrary which standard library methods throw checked exceptions. For instance, why doesn't File.delete (a new Java 7 API does this differently) throw IOException?

另一个值得关注的Hejlsberg为在接受采访时特别指出的是versionability。添加checked异常到罚球子句强制使用该方法的所有code进行修改和重新编译。

Another concern Hejlsberg noted in that interview is versionability. Adding a checked exception to a throw clause forces all code using that method to be modified and recompiled.

这篇关于什么是签入的Java / C#异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆