Hi,
I'm having a weird problem when trying to catch a CRL Exception in AX 2009. I haven't had this problem before with other CLR operations, but when using the System.IO.StreamWriter .NET class and an exception is thrown I'm not able to handle it in a catch statement.
It would be great to have some insight about this.
The following is my code:
try
{
new InteropPermission(InteropKind::ClrInterop).assert();
if(WinApi::fileExists(invoiceFilePath))
WinApi::deleteFile(invoiceFilePath);
sw = new System.IO.StreamWriter(invoiceFilePath); //This is where it crashes and does not fall into the CLRException
//Write in file
….LOGIC HERE (Too long to post it) ….
sw.Flush();
sw.Close();
sw.Dispose();
CodeAccessPermission::revertAssert();
break;
}
}
catch
{
this.eInvoiceProcessTextWriteError();
}
catch(Exception::Internal)
{
this.eInvoiceProcessTextWriteError();
exc = CLRInterop::getLastException();
if( exc )
{
clrExcMessage = exc.get_Message();
// BP Deviation Documented
strError = CLRInterop::getAnyTypeForObject( clrExcMessage );
throw error(strError);
}
}
catch(Exception::CLRError)
{
this.eInvoiceProcessTextWriteError();
exc = CLRInterop::getLastException();
if( exc )
{
clrExcMessage = exc.get_Message();
// BP Deviation Documented
strError = CLRInterop::getAnyTypeForObject( clrExcMessage );
innerExc = exc.get_InnerException();
while(innerExc != null)
{
clrExcMessage = innerExc.get_Message();
// BP Deviation Documented
strError = strError + '\n' + CLRInterop::getAnyTypeForObject( clrExcMessage );
innerExc = innerExc.get_InnerException();
}
throw error(strError);
}
}
catch(Exception::Error)
{
this.eInvoiceProcessTextWriteError();
}
Any help will be appreciated!
*This post is locked for comments
Hi,
No that wasn't the problem. Anyway, I fugured this out and a workaround can be found here:
axwonders.blogspot.com/.../workaround-to-exceptionclrerror-using.html
Thank though!
Hi
First of all, I don´t know if I am correct, but I think that you should sort your "catches" the other way around. The first catch probably catches everything (but I might be wrong...). I would place the specialized catches first.
I tried you code and the only error I could get was when I entered a path that does not exist. That did not cause a CLR error, since the very initialization of the object failed. The error was logged in the infolog, though.
I do however remember that I have had the same problem as you have. The CLR call crashes so hard that the exception don´t even get caught. I just can´t remember why and what my solution was.
Mohamed Amine Mahmoudi
100
Super User 2025 Season 1
Community Member
48
Zain Mehmood
6
Moderator