Is disabling Checked Exceptions in Java possible?

2020-03-23 18:37发布

问题:

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

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

Consider the code snippet below:

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();    
    }
}

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.

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

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

回答1:

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:

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 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.

Interesting read here, here.

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();
  }
}

Use with caution:

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.

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.



回答2:

Other than hacking the compiler there is no option to disable checked exceptions I know of. Best you can do is catch the checked exception and rethrow it within a runtime exception if you don't want to force the client code to handle the checked exception.



回答3:

Use the Manifold compiler plugin with the exceptions plugin option. It effectively neutralizes checked exceptions. With this option checked exceptions behave like unchecked exceptions. No more compiler errors, no more boilerplate try/catch/wrap/rethrow nonsense. Works with Java 8 - 12.