System.Reflection.TargetInvocationException not being caught

NOTE ADDED AFTER SOLUTION: An AccessViolationException was being thrown inside the method called by reflection. This was the reason the TargetInvocationException couldn’t be caught.

NOTE: This is OUTSIDE of the IDE. The referenced question is NOT the same.

  • How can I script the deployment of a Visual Studio database project?
  • How to execute an SSIS package from .NET?
  • Code version control when developing in .NET/SQL Server 2005 on a Mac
  • How to get just that values that are not distinct (count > 1)
  • Mapping Many to Many Relationship w/ Foreign Key Reference
  • Select * from Database 1 and insert into database 2
  • TL;DR

    1. Can’t get a stack trace
    2. Can’t get inner exceptions
    3. Can’t use debugger (copy protection scheme of a third party library gets in the way)
    4. Any changes to the code prevent the exception from occurring – means I can’t add logging to find out where the exception occurrs

    How can I get the exception to be caught or get the needed information some other way?

    Long description:

    I am having a problem with an exception that occurs in a method that gets called by reflection. The exception itself actually occurs in the called method, but since the method is called by reflection, the real exception gets wrapped in a System.Reflection.TargetInvocationException. No problem, just catch it and get the internal exception – except the System.Reflection.TargetInvocationException doesn’t get caught. My program crashes, and I get a dump along with an entry in the Windows event log.

    The Windows event log doesn’t contain the internal exceptions, and neither does the dump.
    I can’t attach a debugger to the program because then an external library (which is needed to get to the reflection call) won’t run – copy protection, don’t you know.
    If I put a try/catch into the offending method, the exception doesn’t occur – which is bad. The cause isn’t fixed, it just doesn’t happen any more.
    The same effect happens if I put logging into the offending method – the exception doesn’t occur any more.

    I can’t use logging, I can’t use a debugger, and in the one place where I could catch the exception and log it the exception doesn’t get caught.

    I’m using Visual Studio 2010 and dotnet 4.0.

    To make it clear: The try/catch doesn’t work when the program is run outside of Visual Studio, and I can’t run it inside of Visual Studio in the debugger because then the program can’t reach the point where the exception occurs. This is NOT within the IDE.

    Eliminating the reflection isn’t an option (I’ve tried it for just the one case, and the exception goes away.)

    The method being called does a lot of stuff, but breaking it down into smaller methods doesn’t help – the exception just goes away.

    The exception doesn’t occur all the time, only when I do a certain sequence of steps – and when it occurs then it is always on the second time through the whole sequence.

    In the sequence I am using, the method gets called by two threads almost simultaneously – a particular bunch of data gets entered, which causes a copy of a report and another document to be printed on two separate printers – one report and document to each printer. Since generating the reports and printing the document can take a while, they are done on threads in the background so the user can continue working.

    I suspect that the threads are stepping on each others toes (lots of file and database manipulations going on) but without knowing what is really happening I can’t fix it.

    The code below shows a simplified version of the call by reflection.

    Does any one have a suggestion as to what may be causing the System.Reflection.TargetInvocationException to not be caught, or maybe an alternative way to catch the inner exception?

        Dim methode As System.Reflection.MethodInfo
        methode = GetType(AParticularClass).GetMethod("OneOfManyMethods", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static)
        Dim resultobject As Object = methode.Invoke(Nothing, Reflection.BindingFlags.InvokeMethod Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static, Nothing, New Object() {SomeBooleanVariable, SomeStringVariable},  Nothing)
        result = DirectCast(resultobject, DataSet)
    Catch ex As Exception
        'Log the error here.
    End Try

  • SQL Server fire off a web request
  • insert record in one table by copying single row of a single column from another table
  • Handle multiple db updates from c# in SQL Server 2008
  • Failed to convert parameter value from a String to a Int32 while executing stored procedure
  • What is the best way to deal with DBNull's
  • Exception “Procedure or function expects parameter, which was not supplied.” thrown even after supplying the parameter
  • 2 Solutions collect form web for “System.Reflection.TargetInvocationException not being caught”

    Found the reason why I couldn’t catch the exception:

    The actual exception was an AccessViolationException, which can’t be caught in dotnet 4.0 without taking special steps (How to handle AccessViolationException.)

    To make things more fun, when AccessViolationException gets thrown in a method called by reflection, only a TargetInvocationException gets logged in the Windows event log, and only the TargetInvocationException is available in the dump.

    Once I managed to get the real exception, I found that that it was a call to Application.DoEvents() from a non-GUI thread that caused the AccessViolation. DoEvents() can cause enough fun when called on the GUI-Thread (Use of Application.DoEvents()), let alone when called from a background thread.

    Once that was fixed, I found that our third party library (the one with the copy protection) doesn’t like being called simultaneously in separate instances. Fixing that was a matter of a synclock in the right place.

    The code causing all of this fun was at one time all in the GUI-Thread, and was originally written back in the days of dotnet 1.1 – this explains the call to DoEvents. The code has been converted piece-wise to run in parallel in background threads through several different stages, with no one single developer having a complete overview of the process.

    I am not sure I can replicate your issue, but this is how we get it and it work’s just fine…

       'YOUR CODE'
     Catch ex As Exception
        'This is where we grab it from... It needs to be in this block to work...
     End Try

    Let me know how it work’s out for you?

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.