9

Checked Exceptions are powerful because they allow you to force the use-site to deal with an exceptional case. If the use-site does not handle an exceptional case (or publically announce that they are not handling it), then the code will fail to compile.

However, that's compile time. What about Runtime?

Are there any meaningful differences between Checked and Unchecked Exceptions at Runtime?

The only thing I can think of is that Unchecked Exceptions extend RuntimeException, but I don't see any properties about RuntimeException that would allow it to be treated differently at RUNTIME.

3
  • 1
    you can see here. Commented Jan 14 at 4:12
  • 1
    @MarcePuente This doesn't really help me. I understand what an Unchecked Exception is, as well as where to use it. My question is about if there are any Runtime characteristic differences between a Checked and Unchecked Exception. Your link does not explain that. Commented Jan 14 at 4:24
  • 1
    Just as an aside, if you look at other JVM languages, such as Scala and Groovy, you will see that all exceptions are unchecked, even if they do not extend RuntimeException Commented Jan 16 at 9:24

2 Answers 2

17

Checked exceptions are a feature of the Java language and only enforced by the compiler. They are not enforced by the Java virtual machine whatsoever.

The throws clause is still recorded in a compiled class file for sake of programming against a compiled library. But in describing the meaning of the data, the JVM spec notes that:

These requirements are not enforced in the Java Virtual Machine; they are enforced only at compile time.

There are various ways to bypass exception checking, such as by abusing generics (which are erased at run time and not fully checked):

class Example {
    public static void main(String[] args) {
        sneakyThrow(new java.io.IOException());
    }
    
    public static void sneakyThrow(Throwable t) {
        Example.<RuntimeException>sneakyThrow0(t);
    }
    
    @SuppressWarnings("unchecked")
    private static <T extends Throwable> void sneakyThrow0(Throwable t) throws T {
        throw (T)t;
    }
}

Output:

Exception in thread "main" java.io.IOException
    at Example.main(Example.java:3)

Also, the reflection method Class.newInstance() is deprecated because due to a design flaw, it rethrows any checked exception thrown by the class's constructor, without wrapping or declaring it:

Deprecated.
This method propagates any exception thrown by the nullary constructor, including a checked exception. Use of this method effectively bypasses the compile-time exception checking that would otherwise be performed by the compiler.

Sign up to request clarification or add additional context in comments.

2 Comments

Woah. That's so scary. So, by doing an unsafe cast, you were able to silence the compiler into allowing the IOException to be treated as if it was a RuntimeException at compile time. And since all you did was throw it, it worked out and just threw an IOException.
Could you paste the output of the main method? I know it's trivial, but it just helps for readability. EDIT -- I see the edit you made. Ty vm. I will accept it once the timer is done, since apparently SO prevents me from accepting so quickly lol.
2

I don't think the JVM makes a distinction between it. The try-with-resources statement, for example, actually creates a throw Throwable statement by the Java compiler.

[2025/1/15] java compiler doesn't create the throw statement. It creates the 'ATHROW' bytecode with a java.lang.Throwable object.

8 Comments

What do you mean by "creates a throw Throwable statement"?
I have the same question as @StephenC. Could you give more details?
Please notify here once you have made the changes.
(If you are refering to the "translation" in JLS 14.20.3.1. Basic try-with-resources ... what Throwable actually means there is "any exception". The true meaning is not literally expressible in real Java. That code should be viewed as pseudo-code.)
@StephenC just like throw (Throwable) throwable;, if you decompile the bytecode
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.