Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is disabling Checked Exceptions in Java possible?

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.

like image 918
NaveeNeo Avatar asked Nov 24 '16 20:11

NaveeNeo


People also ask

Can we ignore checked exception in Java?

No, it raises a compiler error. Being a checked exception, you must either catch it or propagate it by declaring your method as potentially throwing it.

Is it mandatory to handle all checked exception in Java?

A checked exception must be handled either by re-throwing or with a try catch block, a runtime isn't required to be handled. An unchecked exception is a programming error and are fatal, whereas a checked exception is an exception condition within your codes logic and can be recovered or retried from.

Do checked exceptions need to be handled?

Checked exceptions are the subclass of the Exception class. These types of exceptions need to be handled during the compile time of the program. These exceptions can be handled by the try-catch block or by using throws keyword otherwise the program will give a compilation error.

What happens if we don't handle checked exception?

if you don't handle exceptions When an exception occurred, if you don't handle it, the program terminates abruptly and the code past the line that caused the exception will not get executed.


1 Answers

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.

like image 61
lemonsqueeze Avatar answered Sep 20 '22 06:09

lemonsqueeze