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.

  • Using a variable for table name in 'From' clause in SQL Server 2008
  • Full-text search not finding results by removing characters at the font of the string
  • Cheap/Best Dashboard Solution for Microsoft SQL Server Reporting Services and .Net
  • How does SqlDataReader determine whether to set a column to ReadOnly when filling a DataTable?
  • Best method for importing data from text file into MS SQL Server
  • max date record in LINQ
  • 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

  • sort binary datetime
  • SqlTransaction has completed
  • SQL query returning 'Overload resolution error'
  • Entity Framework 5 mixing Oracle & SQL Server
  • Create a database user and login programatically
  • SQL Server .NET SqlBulkCopy class keep running after exception
  • 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.