Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8375657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:13:57+00:00 2026-06-09T15:13:57+00:00

I have a method like this: public void getSomething(){ … } I want to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:13:59+00:00Added an answer on June 9, 2026 at 3:13 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have a method like this on my DocumentFilter public void replaceUpdate(int
If I have a method like this: public void DoSomething(int Count, string[] Lines) {
I have a method to insert data into server like this: public void doInsert(){
Suppose I have a method like this: public void MyCoolMethod(ref bool scannerEnabled) { try
When I have a method like this: public static void foo(String param) throws IOException
I have a fragment (F1) with a public method like this public void asd()
I have a method like this: public void foo(String str){ do.something(); do.anotherthing(); do.somethingelse(); if(str.compareTo(choiceA)==0){
I have a generic method defined like this: public void MyMethod<T>(T myArgument) The first
I have a method which works like this: public void deploy(UserInput userInput) { if
I have a method with declaration like this: public void OriginalMethod(Func<object,bool> selector) And I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.