6

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.

Marco13
  • 53,703
  • 9
  • 80
  • 159
NaveeNeo
  • 205
  • 3
  • 13
  • when writing software you are never sure, that somethin is as you think, it is. So you should allways throw Exceptions if something goes wrong. Why do you want to disable them, if everything is allright, it will never be thrown in other case it should be thrown and you shouldnt disable them – Jarlik Stepsto Nov 24 '16 at 21:00
  • 2
    You may be sure now, but what if that file location will change or will be unavailable (like by disc/file system corruption)? How Java should react in that situation? It got exception from `new FileInputStream("myfile.txt");` but it can't do anything with it... How should your code proceed? Should it go farther and simply ignore any exception? This is asking for trouble. – Pshemo Nov 24 '16 at 21:02
  • how can you be sure? – njzk2 Nov 24 '16 at 21:03
  • My answer is: you should not. Checked Exceptions come from libs to tell the programmer that something is wrong that needs recovering. The programmer can decide if the error can be recovered by logic or if the programs user needs to be informed and maybe asked for a decision. If you turn off checked exceptions you decide to ignore the error. This is the kind of program that just crashed the ExoMars-lander Schiaparelli recently. – Timothy Truckle Nov 24 '16 at 21:04
  • There are tools and libraries that simplify things, for example, https://github.com/google/guava/wiki/ThrowablesExplained#controversial-uses-for-throwablespropagate - but the text there also covers *why* this is "controversial". – Marco13 Nov 24 '16 at 21:16
  • this is an awesome question. C# got it right in this regard. I counted 90% of our 'checked' exceptions should just be runtime(ie. bugs) – Dean Hiller May 13 '17 at 02:06
  • oh, one other answer is scala does away with checked exceptions. – Dean Hiller May 13 '17 at 02:09

2 Answers2

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.

Tom
  • 3,913
  • 19
  • 28
2

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.

Community
  • 1
  • 1
lemonsqueeze
  • 1,041
  • 8
  • 19