Thursday 22 November 2012

Exceptions in Dynamics AX 2012



Exceptions:

       
A Situation in which the flow of a program is interrupted is called exception . A program must handle them For example: If a user requests a record to be fetched and that record doesn't exists then in that case system must throw an exception. There are many types of exceptions thrown depending upon it's cause . All of them can be caught so it's a developer's decision to decide which exceptions must be handled by the system.

Some common exceptions are listed below:







Try , Throw ,Catch & Retry Statements :


Try: 

       These statements indicate the start of the block of code that you are going to handle with X++ Exception handling system . Any exception thrown in this block can be caught in the associated catch statements.

Throw:

       These statements are used to explicitly throw an exception that can be caught by the Catch statement.

Catch:

       These statements define what code is going to be executed in case of an exception being thrown . These statements come after the Try statement. It is not necessary to catch each try statement But each Try statement must have a corresponding Catch statement.

Retry:

       These statements tell the system to go back to the Try statement and attempt to execute the code again , any data loaded before the Try statement remains the same where as any data loaded after the Try statement will be refreshed.

Optimistic Concurrency Control ( OCC ) :


It is a performance enhancing function within Dynamics AX . It assumes that a record retrieved from the database is not updated , until it is proven to be updated by the database which means that in case of updating a record the system compares the rec Version field of the record with the actual record in the database. The rec Version field is updated each time a record is successfully updated. There are two exceptions thrown in case of a record been updated in a transaction using Optimistic Concurrency Control.


 Handling Exception in Code:





























In the above example in the Try block an exception is thrown if the user lefts any dialogue field empty or if the AccountNum already exists. This Exception is then caught in the Catch Block later in the code .

I believe it was helpful.

  

2 comments:

Gautam Verma said...

Helpfull..

cinek433 said...

This is genuinely helpful, thank you!
I think I might be having an issue with my Dynamics software. Whenever I click to open one of the apps, the computer freezes and just won't stop until I shut it down completely.
Do you know what could be the reason?

Post a Comment