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