Nice way to catch CLRError with less lines

When using .NET objects we should think of error handling also.

Previous way to catch CLRErrors looked like this:


catch (Exception::CLRError)
{
ex = ClrInterop::getLastException();
if (ex != null)
{
ex = ex.get_InnerException();
if (ex != null)
error(ex.get_Message()); // Message
}
}

Today I found a method that lets you handle errors with less lines. By simply replacing all the code in the catch-block with a call to AifUtil::getClrErrorMessage().

Making it look a bit cleaner:


catch (Exception::CLRError)
{
error(AifUtil::getClrErrorMessage());
}

Hope to save you some lines of code with this post 😉