Look Mom, No Checked Exception

Published 16 July 07 04:32 AM | andersnoras 

I'm probably showing off an old trick here, but a fierce discussion over at the Norwegian JUG forum reminded me of it, and I thought I'd share it with new dogs who want to learn old tricks.

The cause of the controversy is the ever so popular topic; which is better checked or unchecked exceptions?
Since the release of Java 1.4, a secret class named sun.misc.Unsafe has been hiding within the JDK. You can use this class to pull some amazing tricks, including throwing checked exceptions without having to include these exceptions in the method signature. How? Take a look at this little snippet:

public void LookMomNoCheckedException() // <- No throws clause!
{
    Field field;
    Unsafe unsafe;
    try
    {
        field = Unsafe.class.getDeclaredField("theUnsafe");
        field.setAccessible(true);
        unsafe = (Unsafe) field.get(null);
    }
    catch (Exception e)
    {
        throw new RuntimeException("Sorry, only checked exceptions for you my friend.", e);
    }
    // Let's throw a checked exception...
    unsafe.throwException(new Exception("Look mom no checked exception!"));
}

Voila! You've just thrown a checked exception from within a method without a throws clause - although I think this kind of code will cause even more controversy than the (not so) good old checked exceptions.

Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# WarpedJavaGuy said on July 16, 2007 4:26 PM:

That secret class is actually a non-portable Sun specific implementation.  All vendor specific implementations should be avoided if you want your code to be portable.  Furthermore, vendors can change, remove, and deprecate their internal implementations whenever and however they see fit.

# andersnoras said on July 16, 2007 11:33 PM:

@WarpedJavaGuy;

I know. People should be vary about classes that are only available through reflection, and this post is not meant as a solution people should use.

# WarpedJavaGuy said on July 17, 2007 4:52 AM:

;)

# TrackBack said on July 17, 2007 11:25 AM:
Evil behavior with unchecked checked exceptions
# .k said on July 17, 2007 3:40 PM:

There's also the good old Thread.currentThread().stop(Throwable), if you really want to throw checked exceptions that "can't happen", without having to resort to undocumented API and reflection fun... ;-)

# Anders Norås' Blog said on July 18, 2007 2:01 AM:

My post on throwing undeclared checked exceptions got some attention. Of course you shouldn't use the

# Jon Skeet said on July 20, 2007 1:49 AM:

Just found this post via a series of links. I found a slightly different way of throwing undeclared checked exceptions a while ago:

http://msmvps.com/blogs/jon.skeet/archive/2007/04/03/sheer-evil-rethrowing-exceptions-in-java.aspx

I need to clean up the post at some point to give a full example, but I thought it might be of some interest to some of the readers of this (Anders') post...

# kyb said on September 18, 2007 12:00 AM:

Thread.currentThread().stop(checkedException);

Deprecated, but then your method is in a class called Unsafe.

# andersnoras said on September 18, 2007 8:13 AM:

@kyb;

You're absolutely right about the stop(Exception) method. See this blog post http://andersnoras.com/blogs/anoras/archive/2007/07/17/more-checked-exception-madness.aspx for coverage of this method and unchecked, checked exceptions.

# Exception handling in Java « SoftThink said on December 17, 2007 11:19 AM:

PingBack from http://rattigan.wordpress.com/2007/12/17/exception-handling-in-java/

# Anders Norås' Blog said on January 30, 2008 12:52 PM:

Today it is exactly one year since I pick up on blogging after a long break. To celebrate, I&#8217;ll

Leave a Comment

(required) 
(optional)
(required) 
Enter the code you see below