web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

AX 2012: Handling CLR exceptions

M Anas Khan Profile Picture M Anas Khan 1,424

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");
        }
    }
}

This was originally posted here.

Comments

*This post is locked for comments