是否可以在 Java 中禁用检查异常?
Is disabling Checked Exceptions in Java possible?
我在 Java 中阅读了一篇关于已检查和未检查异常的文章,发现了这个 article/link:
https://projectlombok.org/disableCheckedExceptions.html
根据这篇文章,它只是为 javac 开发的 hack。
考虑下面的代码片段:
import java.io.*;
class Example
{
public static void main(String args[]) throws IOException
{
FileInputStream fis = null;
fis = new FileInputStream("myfile.txt");
int k;
while(( k = fis.read() ) != -1)
{
System.out.print((char)k);
}
fis.close();
}
}
这里我要写public static void main(String args[]) throws IOException
因为我正在尝试打开一个文件。这里"throws"子句是必须的。没有它我会得到一个错误。如果我确定我正在打开的文件存在怎么办。 i.e.myfile.txt 在上述位置。在某些时候,您会感觉到代码不需要很少的检查异常。
Java是否提供了任何工具来根据需要禁用已检查的异常?
即使做了这么多研究,我也找不到合适的答案。
据我所知,除了破解编译器之外,没有其他选项可以禁用已检查的异常。如果您不想强制客户端代码处理已检查的异常,您可以做的最好的事情是捕获已检查的异常并在运行时异常中重新抛出它。
Is there any facility provided by Java to disable checked Exceptions according to the need?
没有官方设施,但有一些解决方法可以在需要时使用:
首先,由于在 java 中同时存在 checked 和 unchecked 异常,使用未检查的异常可能是一种选择。所有派生自 RuntimeException 的异常都未经检查:
RuntimeException is the superclass of those
exceptions that can be thrown during the normal operation of the
Java Virtual Machine.
A method is not required to declare in its throws
clause any subclasses of RuntimeException that might
be thrown during the execution of the method but not caught.
有趣的阅读 here, here。
然后是 type erasure 允许在不声明的情况下抛出已检查的异常。
这就是 lombok 项目用于 @SneakyThrows
的内容
import lombok.SneakyThrows;
public class SneakyThrowsExample implements Runnable {
@SneakyThrows(UnsupportedEncodingException.class)
public String utf8ToString(byte[] bytes) {
return new String(bytes, "UTF-8");
}
@SneakyThrows
public void run() {
throw new Throwable();
}
}
谨慎使用:
Be aware that it is impossible to catch sneakily thrown checked types directly, as javac will not let you write a catch block for an exception type that no method call in the try body declares as thrown.
最后,只有编译器关心已检查的异常,它根本不是 jvm 的一部分。一旦你过了编译器阶段,一切皆有可能。所以直接写字节码或使用 javac 的 checked exceptions disabled 版本完全绕过它们。
我在 Java 中阅读了一篇关于已检查和未检查异常的文章,发现了这个 article/link: https://projectlombok.org/disableCheckedExceptions.html
根据这篇文章,它只是为 javac 开发的 hack。
考虑下面的代码片段:
import java.io.*;
class Example
{
public static void main(String args[]) throws IOException
{
FileInputStream fis = null;
fis = new FileInputStream("myfile.txt");
int k;
while(( k = fis.read() ) != -1)
{
System.out.print((char)k);
}
fis.close();
}
}
这里我要写public static void main(String args[]) throws IOException
因为我正在尝试打开一个文件。这里"throws"子句是必须的。没有它我会得到一个错误。如果我确定我正在打开的文件存在怎么办。 i.e.myfile.txt 在上述位置。在某些时候,您会感觉到代码不需要很少的检查异常。
Java是否提供了任何工具来根据需要禁用已检查的异常?
即使做了这么多研究,我也找不到合适的答案。
据我所知,除了破解编译器之外,没有其他选项可以禁用已检查的异常。如果您不想强制客户端代码处理已检查的异常,您可以做的最好的事情是捕获已检查的异常并在运行时异常中重新抛出它。
Is there any facility provided by Java to disable checked Exceptions according to the need?
没有官方设施,但有一些解决方法可以在需要时使用:
首先,由于在 java 中同时存在 checked 和 unchecked 异常,使用未检查的异常可能是一种选择。所有派生自 RuntimeException 的异常都未经检查:
RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine. A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.
有趣的阅读 here, here。
然后是 type erasure 允许在不声明的情况下抛出已检查的异常。
这就是 lombok 项目用于 @SneakyThrows
import lombok.SneakyThrows;
public class SneakyThrowsExample implements Runnable {
@SneakyThrows(UnsupportedEncodingException.class)
public String utf8ToString(byte[] bytes) {
return new String(bytes, "UTF-8");
}
@SneakyThrows
public void run() {
throw new Throwable();
}
}
谨慎使用:
Be aware that it is impossible to catch sneakily thrown checked types directly, as javac will not let you write a catch block for an exception type that no method call in the try body declares as thrown.
最后,只有编译器关心已检查的异常,它根本不是 jvm 的一部分。一旦你过了编译器阶段,一切皆有可能。所以直接写字节码或使用 javac 的 checked exceptions disabled 版本完全绕过它们。