22

I have a method like this:

public void getSomething(){
...
}

I want to throw an Exception inside getSomething(). The compiler will not allow me to do that because my method doesn't allow Exception to be thrown in there. But I need to throw a subclass of Exception for my testing (I can't throw Unchecked Exception). This is clearly a hack but I need it for my testing. I tried EasyMock but it doesn't allow me to do that either. Any ideas how to do that?

Thanks, Sean Nguyen

Sean Nguyen
  • 12,528
  • 22
  • 74
  • 113
  • You are testing something that is impossible to happen. Why? – rodrigoap Aug 13 '12 at 22:13
  • I tried to mock soap response using LogicalHandler. The handleMessage(LogicalMessageContext context) doesn't declare throws Exception. But all the exception throws by the soap end point are subclass of Exceptions. I need to throw those exception to mock my service response. – Sean Nguyen Aug 13 '12 at 22:16
  • See my edit in case you're interested. – Paul Bellora Sep 06 '12 at 00:31
  • Why you couldn't throw Unchecked exception? I can't understand what you were trying to do. What kind of test framework are you using? You can catch unchecked exceptions normally in your tests. – ceklock Sep 15 '13 at 21:18

3 Answers3

26

Method 1:

This post by Alexey Ragozin describes how to use a generics trick to throw an undeclared checked exception. From that post:

public class AnyThrow {

    public static void throwUnchecked(Throwable e) {
        AnyThrow.<RuntimeException>throwAny(e);
    }

    @SuppressWarnings("unchecked")
    private static <E extends Throwable> void throwAny(Throwable e) throws E {
        throw (E)e;
    }
}

The trick relies on throwUnchecked "lying" to the compiler that the type E is RuntimeException with its call to throwAny. Since throwAny is declared as throws E, the compiler thinks that particular call can just throw RuntimeException. Of course, the trick is made possible by throwAny arbitrarily declaring E and blindly casting to it, allowing the caller to decide what its argument is cast to - terrible design when coding sanely. At runtime, E is erased and has no meaning.

As you noted, doing such a thing is a huge hack and you should document its use very well.


Method 2:

You can also use sun.misc.Unsafe to this end. First you must implement a method that uses reflection to return that class's instance:

private static Unsafe getUnsafe() {
    try {
        Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe");
        theUnsafeField.setAccessible(true);
        return (Unsafe)theUnsafeField.get(null);
    }
    catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    }
    catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

This is necessary as calling Unsafe.getUnsafe() will typically throw a SecurityException. Once you have the instance of Unsafe you can put its terrifying capabilities to use:

Unsafe unsafe = getUnsafe();
unsafe.throwException(new Exception());

Credit goes to this answer on the post https://stackoverflow.com/questions/5574241/interesting-uses-of-sun-misc-unsafe. I thought I'd mention this for completeness but it's probably better just to use the trick above instead of allowing Unsafe into your code.


Method 3:

In the comments of the linked answer about using Unsafe, @bestsss points out a much simpler trick using the deprecated method Thread.stop(Throwable):

Thread.currentThread().stop(new Exception());

In this case you would use @SuppressWarnings("deprecation") and once again document very fiercely. Again, I prefer the first trick for its (relative) cleanliness.

Community
  • 1
  • 1
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
  • @PaulBellora you could always play around with the `Class::newInstance()` method, that can will throw anything the ctor does – emesx Jun 16 '13 at 08:15
  • @PaulBellora Why doesn't the first example result in `ClassCastException` at runtime? – Demi Apr 26 '14 at 15:50
  • @Demetri `(E)e` is an unchecked cast, meaning the JVM cannot fail fast if it's wrong. After type erasure the method is indistinguishable from `private static void throwAny(Throwable e) throws Throwable { throw e; }`. – Paul Bellora Apr 26 '14 at 20:07
  • `Unsafe` is just fine – Enerccio Sep 12 '17 at 06:50
  • @Enerccio until Java 9 – Paul Bellora Sep 12 '17 at 19:41
  • Method 3 no longer works since JDK 8, because `stop(Throwable)` methow now just throw exception to the caller. – Beholder Oct 30 '17 at 18:22
  • @PaulBellora but if you replace you get a cast exception at runtime given an IOException, why is that? – Ludvig W Oct 31 '21 at 11:00
  • @LudvigW the compiler's type erasure process replaces the generic type E with its upper bound. When that upper bound is Throwable, the cast works. When it's RuntimeException, it doesn't. – Paul Bellora Nov 01 '21 at 13:44
5

Java has two kinds of exceptions, checked and unchecked. You must declare checked exceptions, but you don't have to declare unchecked exceptions.

RuntimeException is the basic unchecked exception, so you can throw that without declaring it.

public void getSomething(){
   throw new RuntimeException("I don't have to be declared in the method header!");
}

As a side note, you probably don't want to throw a raw RuntimeException, but subclass it to something more specific to your needs. Any subclass of RuntimeException will be unchecked as well.

Oleksi
  • 12,947
  • 4
  • 56
  • 80
  • I am sorry for not being clear. I need to throw a subclass of java.lang.Exception (checked exception). – Sean Nguyen Aug 13 '12 at 22:11
  • 3
    @SeanNguyen RuntimeException is a subclass of Exception. It's just a special case in Java where you don't need to declare that it'll be thrown. – Oleksi Aug 13 '12 at 22:12
  • Sorry, not very clear again. I need to throw checked exception that is subsclass of Exception. I can't throw RuntimeException. – Sean Nguyen Aug 13 '12 at 22:22
  • @SeanNguyen: please tell us why you need to throw a checked exception. – ceklock Sep 15 '13 at 21:20
0

I don't know what you were trying to do. You could just throw an unchecked exception inside your method and then test using JUnit like in my example below.

public void getSomething() {
    throw new RuntimeException();
}

JUnit 4 test example:

@Test
public void testGetSomething() {
    try {
        getSomething();
        fail("Expecting RuntimeException!");
    } catch (Exception e) {
        Logger.getLogger("test").info("Exception was caught: " + e.getClass());
    }
}
ceklock
  • 6,143
  • 10
  • 56
  • 78