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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:13:39+00:00 2026-06-01T23:13:39+00:00

In my code I’m coming across a situation in which a System.Reflection.TargetInvocationException is thrown.

  • 0

In my code I’m coming across a situation in which a System.Reflection.TargetInvocationException is thrown. In one specific case I know how I want to handle the root exception, but I want to throw all other exceptions. I can think of two ways of doing this, but I’m not sure which is better.

1.

try
{
    //code
}
catch (System.Reflection.TargetInvocationException ex)
{
    if (typeof(ex.InnerException) == typeof(SpecificException))
    {
        //fix
    }
    else
    {
        throw ex.Innerexception;
    }
}

2.

try
{
    //code
}
catch (System.Reflection.TargetInvocationException ex)
{
    try
    {
        throw ex.InnerException;
    }
    catch (SpecificException exSpecific)
    {
        //fix
    }
}

I’m aware that throwing exceptions in general is slow, so I feel the first method would possibly be faster. Alternatively, is there a better way of doing this that I haven’t thought of?

  • 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-01T23:13:40+00:00Added an answer on June 1, 2026 at 11:13 pm

    Each of your proposed solutions has its own issue.

    The first method checks that the type of the inner exception is exactly the type you’re expected. That means that a derived type won’t match, which might not be what you intended.

    The second method overwrites the inner exception’s stack trace with the current stack location, as Dan Puzey mentioned. Destroying the stack trace may be destroying the one lead you require in order to fix a bug.

    The solution is basically what DarkGray posted, with Nick’s suggestion and with an added suggestion of my own (in the else):

    try 
    { 
        // Do something
    } 
    catch (TargetInvocationException ex) 
    { 
        if (ex.InnerException is SpecificException) 
        { 
            // Handle SpecificException
        }
        else if (ex.InnerException is SomeOtherSpecificException)
        {
            // Handle SomeOtherSpecificException
        }
        else 
        { 
            throw;    // Always rethrow exceptions you don't know how to handle.
        } 
    }
    

    If you want to re-throw an exception that turns out you can’t handle, don’t throw ex; since that will overwrite the stack trace. Instead use throw; which preserves the stack trace. It basically means “I actually didn’t want to enter this catch clause, pretend I never caught the exception”.

    Update: C# 6.0 offers a much better syntax via Exception Filters:

    try
    {
        // Do something
    }
    catch (TargetInvocationException ex) when (ex.InnerException is SpecificException)
    {
        // Handle SpecificException
    }
    catch (TargetInvocationException ex) when (ex.InnerException is SomeOtherSpecificException)
    {
        // Handle SomeOtherSpecificException
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code static void MyClass::ThreadEntryStatic() { //... } void MyClass::Begin() { CreateThread(..,ThreadEntryStatic,..); } In which
//CODE $entries = $model->fetchEntries(); $feedUri = '/rss/'; //link from which feed is available $link
Code: %a = ( 1 => ONE , 2 => TWO , 3 =>
Code example: http://jsfiddle.net/MhEPw/1/ I have two jQuery Deferred objects. I want to have more
Code below works well on Firefox - displays progress bar which progresses on every
CODE:1 class Ajay { private void display() { System.out.println(Ajay); } public static void main(String
Code style advice, please: I want to prevent the rampant distribution of selector strings,
Code that was working fine last week is suddenly throwing this exception: System.Data.SqlClient.SqlException (0x80131904):
Code for server: http://stikked.com/view/64826511 Network Code for client: http://stikked.com/view/38974838 Basically, the client connects to
Code in the book Beginning iPhone Development (by Dave Mark & Jeff LaMarche) assigns

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.