but your version has the disadvantage of being visible i (and i hope christian too) was hoping for a more magical way of throwing checked exceptions
of course you shouldnt use those in an actual app On Wed, Aug 26, 2009 at 5:36 AM, Reinier Zwitserloot<reini...@gmail.com> wrote: > > WTF? I already posted a much better version of this in the > supermegauber thread. no need for class file hacking. sun.misc.Unsafe > is an even worse idea (Security Manager issues, as well as a > dependency on running in sun VMs). > > public class SneakyThrow { > public static RuntimeException sneakyThrow(Throwable t) { > if ( t == null ) throw new NullPointerException("t"); > SneakyThrow.<RuntimeException>sneakyThrow0(t); > return null; > } > > �...@suppresswarnings("unchecked") > private static <T extends Throwable> void sneakyThrow0(Throwable t) > throws T { > throw (T)t; > } > } > > > Note also how this is much more thought through: Java does not know > that calling this method automatically triggers a throw statement, so > the compiler will whine that you need to return something, and the DA > rules are all messed up. Therefore, the suggested usage is: > > public int myMethod() { > throw sneakyThrow(new IOException()); > } > > Note the 'throw' in front of 'sneakyThrow'. If you've read up on your > JLS and JVMS, you'll know that this code is perfectly valid java (and, > given sun's dogged adherence to backwards compatibility, should mean > it'll continue to work just fine), and that it'll work on every java- > compatible VM. > > On Aug 26, 5:54 am, Christian Catchpole <christ...@catchpole.net> > wrote: >> Yeah, i was reading about that one. But it's only in the Sun VMs and >> probably subject to change. >> >> But hey, I just listed this as an exercise. It just shows the >> difference between checked and unchecked is one little byte. :) >> >> On Aug 26, 1:44 pm, Marcelo Fukushima <takesh...@gmail.com> wrote: >> >> >> >> > theres also a throwException(Throwable) in sun.misc.Unsafe - though to >> > use that you really have to want to >> >> > On Wed, Aug 26, 2009 at 12:30 AM, Christian >> >> > Catchpole<christ...@catchpole.net> wrote: >> >> > > Compile this.. (any package you like, or no package at all) >> >> > > public class Rethrow { >> > > public static void unchecked(Throwable t) { >> > > t=t; >> > > } >> > > } >> >> > > javap reports the byte code as.. >> >> > > public static void unchecked(java.lang.Throwable); >> > > Code: >> > > Stack=1, Locals=1, Args_size=1 >> > > 0: aload_0 >> > > 1: astore_0 >> > > 2: return >> >> > > which in hex is: >> >> > > 2A 4B B1 >> >> > > open the class file in the hex editor, search for that and change it >> > > to: >> >> > > 2A BF B1 >> >> > > javap now reports the byte code as.. >> >> > > public static void unchecked(java.lang.Throwable); >> > > Code: >> > > Stack=1, Locals=1, Args_size=1 >> > > 0: aload_0 >> > > 1: athrow >> > > 2: return >> >> > > jar that class up or otherwise protect it from re-write. >> >> > > In your code you can now call this without wrapping with a runtime >> > > exception. And the stack trace is still that of the original >> > > exception. >> >> > > } catch(Exception e) { >> > > Rethrow.unchecked(e); >> > > } >> >> > > Obviously, use at your own risk. No warrenties etc. :) >> >> > --http://mapsdev.blogspot.com/ >> > Marcelo Takeshi Fukushima > > > -- http://mapsdev.blogspot.com/ Marcelo Takeshi Fukushima --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---