Examined Exception

Continued: Elusive Exception, Part 2

I am writing a custom binding mechanism; My converter is called before the DataContext is set for the target element. This alone does not really matter, because it will be updated when the DataContext ultimately gets the value. What causes the problems is the NullReferenceException, which I get because the DataContext is null, which doesn't seem to want to be caught.

Despite the fact that I'm trying to catch an exception in the value converter:

try { return ( (MethodInfo)_member ).Invoke( parameter, null ); } catch { return null; } 

For some reason, the debugger still stops at this point.

alt text

(This backs up the stack trace a little to where the catch block is located - the actual exception occurs inside the _member method. The weird part is that the catch block is inactive, but the breakpoint is never reached.)

Now I think that this may be due to the fact that the exception occurs in another assembly, from where it is caught (I try to pack this into the reusable class library, and _member above points to the method in the application assembly).

If I run my small test application without a debugger, it works fine, however, my application is a little more robust and has general exception handling, which starts because of this.

I am wondering if there is any attribute or something (or maybe some reflection parameter that I skip?) That I can use so that the exception is caught, as it should be.

Update: I am pretty sure that this should be due to the reflection and use of MethodInfo.Invoke. It appears that the exception is the first of "TargetInvocationException" with an internal exception from NullReferenceException. It seems that the call exception somehow happens outside the call stack and therefore is not intercepted inside it. I am not doing anything with threads, but maybe some implicit thread offset is happening inside MethodInfo.Invoke?

Does anyone have any ideas how I could get this to be caught, or perhaps another way to call a method from a method name that won't have this problem? I am partly in a dead end.

+4
source share
2 answers

Check the debugger exception settings to see if you want to debug the debugger when a NullReferenceException is NullReferenceException .

+1
source

I am sure that you can catch exceptions just fine after the call and there is no mechanism specific to reflection needed to find it.

Is the method you use using streams and throwing an exception to the child thread used? This may cause your try-catch to skip the exception if the exception occurs in any non-black thread that has not been completed before you leave the try-catch statement.

0
source

Source: https://habr.com/ru/post/1312926/


All Articles