Purpose:
The purpose of this post is to demonstrate how can we effectively handle CLR exceptions.
Application:
Dynamics AX 2012
Business requirement:
To report CLR inner exception.
Solution:
Please find the code below to get the inner CLR exception.
Code
private void getFiles()
{
System.String[] files;
System.Collections.IEnumerator enumerator;
str file;
;
try
{
fileSet = new Set(Types::Container);
files = System.IO.Directory::GetFiles(filePath, '*.csv');
enumerator = files.GetEnumerator();
while (enumerator.MoveNext())
{
file = enumerator.get_Current();
fileSet.add([file]);
}
}
catch (Exception::Internal)
{
this.processCLRErrorException();
}
catch (Exception::CLRError)
{
this.processCLRErrorException();
}
}
private void processCLRErrorException(boolean _throw = true)
{
CLRObject exc;
CLRObject innerExc;
CLRObject clrExcMessage;
str strError;
;
exc = CLRInterop::getLastException();
if (exc)
{
innerExc = exc.get_InnerException();
clrExcMessage = exc.get_Message();
while (innerExc != null)
{
innerExc = innerExc.get_InnerException();
clrExcMessage = innerExc.get_Message();
}
strError = CLRInterop::getAnyTypeForObject(clrExcMessage);
error(strError);
if (_throw)
{
throw error("Update has been cancelled");
}
}
}