Wednesday, May 18, 2022

Exceptional handling - Try Catch Throw in Dynamics 365 FO

try

{

***********

***********

***********

***********

}

catch(Exception::Error)

{

    System.Exception ex = CLRInterop::getLastException();

    error(ex.Message);

}

 






try, catch, finally, and retry statements

try

{

    // Code here.

}

catch (Exception::Numeric)

{

    info("Caught a Numeric exception.");

}

catch

{

    info("Caught an exception.");

}

finally

{

    // Executed no matter how the try block exits.

}

 






Exception for throwing/saving error message

#1

System.ArgumentException ex;

try

{

    throw new System.ArgumentException("Invalid argument specified");

}

catch(ex)

{

    error(ex.Message);

}

#2

try

{

    // call to .NET code which throws exception

}

catch(Exception::CLRError)

{

    System.Exception ex = CLRInterop::getLastException();

    error(ex.Message);

}

 






Handling a CLRError

// This example shows that a CLRError exception is not displayed

// in the Infolog unless you catch the exception and manually

static void TryCatchCauseCLRError3Job(Args _args)

{

    System.String netString = "Net string.";

    System.Exception netExcepn;

    try

    {

        info("In the 'try' block. (j3)");

        netString.Substring(-2); // Causes CLR Exception.

    }

    catch (Exception::Error)

    {

        info("Caught 'Exception::Error'.");

    }

    catch (Exception::CLRError)

    {

        info("Caught 'Exception::CLRError'.");

        netExcepn = CLRInterop::getLastException();

        info(netExcepn.ToString());

    }

 






List of exceptions :

The following table shows the exception literals that are the values of the Exception enumeration.

Break -------- The user pressed Break or Ctrl+C.

CLRError -------- An error occurred while the CLR functionality was being used.

CodeAccessSecurity -------- An error occurred while the CodeAccessPermission.demand method was being used.

DDEerror -------- An error occurred while the DDE system class was being used.

Deadlock -------- A database deadlock occurred, because several transactions are waiting for each other.

DuplicateKeyException -------- An error occurred in a transaction that is using Optimistic Concurrency Control. The transaction can be retried (use a retry statement in the catch block).

DuplicateKeyExceptionNotRecovered -------- An error occurred in a transaction that is using Optimistic Concurrency Control. The code won't be retried. This exception can't be caught inside a transaction.

Error -------- A fatal error occurred. The transaction has been stopped.

Info -------- This exception literal holds a message for the user. Don't throw an info exception.

Internal -------- An internal error occurred in the development system.

Numeric -------- An error occurred while the str2int, str2int64, or str2num function was being used.

UpdateConflict -------- An error occurred in a transaction that is using Optimistic Concurrency Control. The transaction can be retried (use a retry statement in the catch block).

UpdateConflictNotRecovered -------- An error occurred in a transaction that is using Optimistic Concurrency Control. The code won't be retried. This exception can't be caught within a transaction.

Warning -------- An exceptional event has occurred. Although the user might have to take action, the event isn't fatal. Don't throw a warning exception.

SQL connection error X++ exception -------- An error occurred when during the query execution. The transaction will be canceled. This exception can't be caught within a transaction.

Link