I really hate phrasing something like this as being a "figment."  That's
like saying that "short" and "char" are figments of the JVM's imagination
because it's all just bits.

None-the-less, the answer is that checked exceptions are enforced by Java's
static system and not by the JVM at all.  So Scala, Groovy, JRuby, etc
pretty much just ignore them.

Reinier posted a very clever way to take advantage of the fact that casts to
type parameters aren't checked to effectively cast away the "checkedness" of
an exception without wrapping it, which I'll copy in case you can't find it

public class Sneak {
    public static RuntimeException sneakyThrow(Throwable t) {
        Sneak.<RuntimeException>sneakyThrow0(t);
        return null;
    }

    @SuppressWarnings("unchecked")
    private static <T extends Throwable> void sneakyThrow0(Throwable t)
            throws T {
        throw (T) t;
    }

}


It gets used like

public class Test {
    public void test() {
        throw sneakyThrow(new IOException("this should be checked, but it's
not!"));
    }

    public static void main(String[] args) {
        try {
            new Test().test();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

It is mildly annoying that you can't catch IOException here because Java
says it can't possibly happen.  Silly Java, we just pervted you so that it
can!

On Tue, Aug 18, 2009 at 6:17 PM, Christian Catchpole <
christ...@catchpole.net> wrote:

>
> People have suggested similar keywords - I don't like the idea of
> wrapping in a RuntimeException, but as we know, checked exceptions are
> a figment of Java's imagination and are simply enforced by the
> compiler.
>
> Why not simply allow them to bubble up as their original exception,
> just remove the need for checking them.
>
> void thing() coverts IOException {
> }
>
> Does anyone know, if you were to remove the throws declarations from a
> method in a class, would the JVM still validate the class file, if the
> method was clearly throwing checked exceptions?  I assume it would
> allow it.  I guess this is how Scala works.
>
> On Aug 19, 11:06 am, Alex Buckley <alex.buck...@sun.com> wrote:
> > I'm slightly embarrassed to admit I'm a fan ofhttp://
> bugs.sun.com/view_bug.do?bug_id=6534270
> >
> > On Aug 15, 5:56 am, Jeff Grigg <jeffgr...@charter.net> wrote:
> >
> >
> >
> > > I like the beauty and simplicity of completely empty catch blocks.
>  >;->  OK, some developers, to comply with corporate documentation
> >
> > > standards, put comments there.   >;->
> >
> > > (Reality is that I'll usually wrap checked exceptions in
> > > RuntimeExceptions at a low level, and then catch Exception at the top
> > > level -- to log it and abort or retry the transaction or user action.
> > > There's a lot of really bad code out there that misuses exceptions and
> > > does exception handling cleanup wrong.  It's a problem!!)
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to javaposse@googlegroups.com
To unsubscribe from this group, send email to 
javaposse+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to