是否禁用Java中的检查异常? [英] Is disabling Checked Exceptions in Java possible?

查看:213
本文介绍了是否禁用Java中的检查异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关已检查和未检查的Java异常的文章,并发现本文/链接:
https://projectlombok.org/disableCheckedExceptions.html

I was reading an article about checked and unchecked Exceptions in Java and found this article/link: https://projectlombok.org/disableCheckedExceptions.html

根据文章,这只是为javac开发的一个黑客。

According to the article it's just a hack developed for javac.

考虑下面的代码片段:

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子句是必须的。没有它,我会得到一个错误。如果我确信我打开的文件的存在,该怎么办? iemyfile.txt在所提及的位置。在某些时候,可以感觉到代码不需要很少的检查异常。

Here I have to write public static void main(String args[]) throws IOException because I am trying to open a file. Here "throws" clause is a must. Without it I will get an error. What if I am sure about the Existance of the file I am opening. i.e.myfile.txt in the mentioned location. At some point one can feel that few Checked Exceptions are not required for the code.

Java提供的任何工具是否根据需要禁用所选的异常?

即使做了这么多研究,我找不到适当的答案。

Even after doing so much research I could not find a proper answer for it.

推荐答案


Java提供的任何工具是否根据需要禁用所选的异常?

Is there any facility provided by Java to disable checked Exceptions according to the need?

没有官方设施,但有需要时可以使用的解决方法:

No official facilities, but there are workarounds one can use when needed:

首先,由于选中未选中的异常,使用未选中的异常可能是一个选项。从RuntimeException派生的所有异常都将被取消选中:

First, since there are both checked and unchecked exceptions in java, using unchecked exceptions might be an option. All exceptions which derive from RuntimeException are unchecked:


RuntimeException是在正常操作期间抛出的
异常的超类的
Java虚拟机。
一个方法不需要在它的throws中声明
子句任何可能
的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

然后有类型擦除,允许抛出被检查的异常而不声明它。

这是项目lombok用于 @ SneakyThrows

Then there is type erasure which allows to throw a checked exception without declaring it.
This is what project lombok uses for @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();
  }
}

谨慎使用:


请注意,不可能直接捕捉被偷看的类型,因为javac不会让您为尝试中没有方法调用的异常类型写入catch块身体宣称抛出。

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版本完全绕过它们。

And lastly it's only the compiler which cares about checked exceptions, it's not part of the jvm at all. Once you're past the compiler stage anything is possible. So writing bytecode directly or using a version of javac with checked exceptions disabled completely bypasses them.

这篇关于是否禁用Java中的检查异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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