Handling .Net exceptions with X++
In my last blog I was talking about casting CLR-objects in X++. In the code example you might have noticed the following code:
3: //we'll discuss this later...
Well, handling CLR-objects isn’t that easy. But fortunately you can find examples in Ax (for example in SysWorkflowHelper/validateWorkflowRuntimeURL) like the following code (which I modified a little bit) :
1: System.Exception clrException;
3: // your code goes here
7: clrException = CLRInterop::getLastException();
8: if (clrException != null)
10: clrException = clrException.get_InnerException();
11: if (clrException != null)
In line 9 we get the .Net exception that caused the error in X++, but it will always be the message:
"target of invocation failed...."
and so is of little interest. What is much more interesting is the exception that is wrapped by the System.Reflection.TargetInvocationException, and that is done in line 10 by calling the method “get_InnerException”-method. That’s the exception which will give you an idea of your real problem…