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 7786613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:26:42+00:00 2026-06-01T20:26:42+00:00

I start right with the code. This one invokes a method using reflection try

  • 0

I start right with the code. This one invokes a method using reflection

try {
    Method method = states.getClass().getDeclaredMethod(
            getCurrentStateId() + "_" + request.getEvent());
    states.setData(request, dataManager);
    method.invoke(states);
} catch (NoSuchMethodException e) {
    logger.debug("Method " + getCurrentStateId() + "_" + request.getEvent()
            + " cannot be found - invocation not performed.", e);
} catch (IllegalArgumentException e) {
    throw new InternalException("Method invocation with reflection failed.", e);
} catch (IllegalAccessException e) {
    throw new InternalException("Method invocation with reflection failed.", e);
} catch (InvocationTargetException e) {
    throw new InternalException("Method invocation with reflection failed.", e);
}

and calls the method with the following code which throws a PropertiesDontMatchException (runtime).

...
if (totalCredits < minimumCredits || totalCredits > maximumCredits) {
    throw new PropertiesDontMatchException("Minimum amount of credits=" + minimumCredits
            + ", maximum amount of credits=" + maximumCredits + ". Your amount of credits="                 + totalCredits + ". You have to modify your set of subjects.");
}
...

The thing is that my runtime exception is being wrapped into InvocationTargetException and caught in the first code snippet. This is not what I want. But according to the documentation, it’s the correct behaviour however.

So I came up with this solution

...
} catch (InvocationTargetException e) {
    if (e.getCause() instanceof PropertiesDontMatchException) {
        throw (PropertiesDontMatchException) e.getCause();
    }
    throw new InternalException("Method invocation with reflection failed.", e);
}
...

Is this the proper way how to propagate my runtime exception or is there any better solution of this problem?

  • 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-01T20:26:43+00:00Added an answer on June 1, 2026 at 8:26 pm

    Yes, this is the correct error handling in this case. I would only extend your test for any RuntimeException:

    } catch (InvocationTargetException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        if (e.getCause() instanceof Error) {
            throw (Error) e.getCause();
        }
        throw new InternalException("Method invocation with reflection failed.", e);
    }
    

    Or use Throwables utility class from guava instead if you don’t mind using RuntimeException instead of custom InternalException for checked exceptions:

    } catch (InvocationTargetException e) {
        throw Throwables.propagate(e.getCause());
    }
    

    The extra wrapping is necessary to distinguish between your e.g. your method throwing IllegalAccessException and reflection mechanism itself throwing it.

    Similar API design choice can be observed with Future.get() – in case of exception thrown from asynchronous job ExecutionException is thrown wrapping the actual exception.

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

Sidebar

Related Questions

Right now we just use something like this stopWatch.Start(); try { method(); } finally
First one: how can I create a Thread that doesn't start right away.If I
Right - to start with, I'm entering unfamiliar areas with this - so please
When I start a drag and drop using a right click in WPF it
Why doesn't this work right? function test() { var start = new Date(2012, 3,
I have this form right here, where the user will add the start date
I have one page A having this code snippet if (!var) { header(Location: .URL,
Quite stumped on this one. I am using the Youtube JS API to determine
I have this code that will load css/js/jst (javascript templates) using $.ajax and I'd
I'm using this code to make #sidebar 's height match #post_content 's height: <script

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.