Quick Post: Catching ClrErrors in X++
I wanted to do a quick post actually with some X++ code. I know this exists already on the web somewhere, but it's always handy code when working with .Net Assemblies, WCF Services, etc.
So the following is a great way to catch and see what all is going on with any .Net error being thrown.:
System.Execption ex;
str ClrErrCatch;
;
try
{
// Do some code work here...
}
catch (Exception::ClrError)
{
ex = ClrInterop::getLastException();
if (ex != null)
{
ClrErrCatch = ex.get_Message();
ClrErrCatch += ex.get_StackTrace();
ex = ex.get_InnerException();
if (ex != null)
{
ClrErrCatch += ex.ToString();
}
}
error(ClrErrCatch);
}
Now the above assumes your making correct use of CAS, or Code Access Security, with an InterOpPermission.Assert() going on.
The point with this post is to give a good template for working with Clr errors. This includes anytime you reference any of the System.* .Net Assemblies, your own custom .Net assemblies, and also Service references of any type, be that Web or WCF.
Check back soon, as more good post are coming on. Also this can be included in our WCF series of post going on as well.
"Visit the Dynamics AX Community Page today!"
So the following is a great way to catch and see what all is going on with any .Net error being thrown.:
System.Execption ex;
str ClrErrCatch;
;
try
{
// Do some code work here...
}
catch (Exception::ClrError)
{
ex = ClrInterop::getLastException();
if (ex != null)
{
ClrErrCatch = ex.get_Message();
ClrErrCatch += ex.get_StackTrace();
ex = ex.get_InnerException();
if (ex != null)
{
ClrErrCatch += ex.ToString();
}
}
error(ClrErrCatch);
}
Now the above assumes your making correct use of CAS, or Code Access Security, with an InterOpPermission.Assert() going on.
The point with this post is to give a good template for working with Clr errors. This includes anytime you reference any of the System.* .Net Assemblies, your own custom .Net assemblies, and also Service references of any type, be that Web or WCF.
Check back soon, as more good post are coming on. Also this can be included in our WCF series of post going on as well.
"Visit the Dynamics AX Community Page today!"
Labels: .Net, Assert, CAS, DotNet, Dynamics AX 2009, Service Reference, WCF, Web Services, X++
1 Comments:
Hi there Brandon,
I'm trying to figure out why your variable ex would ever be null. You test for this in your catch block. It implies ClrInterop::getLastException() can return null, as in fact I've found. However, this isn't what I was expecting and it is quite a problem for me.
Is there some environment setting that could be involved?
Post a Comment
<< Home