Error System.Reflection.TargetInvocationException

NOTE ADDED AFTER SOLUTION: An AccessViolationException is thrown in a method called by reflection. This was the reason that TargetInvocationException could not be caught.

NOTE. These are EXTERNAL IDEs. The above question is NOT the same.

TL DR

  • Unable to get stack trace
  • Unable to get internal exceptions
  • Cannot use debugger (third-party library copy protection scheme interferes)
  • Any changes to the code prevent an exception from occurring - which means I cannot add a log to find out where the exception occurs

How can I make an exception catch or get the necessary information in another way?

Long description:

I had a problem with an exception that occurs in a method that is called by reflection. The exception itself does indeed occur in the called method, but since the method is called by reflection, the real exception is wrapped in a System.Reflection.TargetInvocationException . No problem, just catch it and get an internal exception, except that System.Reflection.TargetInvocationException not caught. My program crashes and I get a dump along with the entry in the Windows event log.

The Windows event log does not contain internal exceptions, as well as a dump. I cannot attach a debugger to the program, because then the external library (which is needed to call reflection) will not work - copy protection, don't you know. If I put try / catch in an insult method, an exception will not occur - this is bad. The reason is not fixed, it no longer exists. The same effect occurs if I put an input into the method of insult - an exception no longer occurs.

I cannot use logging, I cannot use debugger, and in one place where I could catch an exception and register it, the exception does not come across.

I am using Visual Studio 2010 and dotnet 4.0.

To make it clear: the / catch attempt does not work when the program starts outside of Visual Studio, and I cannot run it inside Visual Studio in the debugger, because then the program cannot reach the point where the exception occurs. It is NOT in the IDE.

Eliminating reflection is not an option (I tried it for only one case, and the exception goes away.)

The called method does a lot of things, but breaking it into smaller methods does not help - the exception just disappears.

An exception does not occur all the time, only when I perform a certain sequence of steps - and when this happens, it is always the second time through the entire sequence.

In the sequence that I use, the method is called by two threads almost simultaneously - a specific group of data is entered, which forces you to copy the report and another document to two separate printers - one report and a document for each printer. Since it may take some time to generate reports and print a document, they run in threads in the background so that the user can continue to work.

I suspect that threads are advancing on each other (a lot of operations with files and databases are happening), but not knowing what is really happening. I can’t fix it.

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

Does anyone have a suggestion on what could cause System.Reflection.TargetInvocationException not get caught, or maybe an alternative way to catch an internal exception?

 Try 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 
+5
source share
2 answers

Found the reason why I could not catch the exception:

The actual exception was an AccessViolationException, which cannot be detected in dotnet 4.0 without taking special steps ( How to handle an AccessViolationException. )

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

As soon as I managed to get a real exception, I found that it was a call to Application.DoEvents () from a non-GUI thread that called AccessViolation. DoEvents () can be quite fun when calling a GUI-Thread ( Using Application.DoEvents () ), not to mention calling from a background thread.

As soon as this was fixed, I found that our third-party library (one with copy protection) did not like to be called in separate copies at the same time. Commit is a matter of synchronization in the right place.

The code that caused all this fun was at the same time in the GUI-Thread, and was originally written during dotnet 1.1 - this explains the call to DoEvents. The code was converted in parts for parallel work in the background thread through several different stages, and no developer has a complete overview of the process.

+8
source

I'm not sure I can replicate your problem, but this is how we get it, and it works fine ...

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

Let me know how it works for you?

0
source

All Articles