IT tutorials
 
Applications Server
 

Microsoft Dynamic AX 2009 : Syntax (part 3) - Statements - Exception Handling

10/1/2011 5:50:11 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
Exception Handling

It is a best practice to use the X++ exception handling framework instead of programmatically halting a transaction by using the ttsabort statement. An exception (other than the update conflict exception) thrown inside a transaction block halts execution of the block, and all the inserts and updates performed since the first ttsbegin statement are rolled back. Throwing an exception has the additional advantage of providing a way to recover object state and maintain database transaction consistency. Inside the catch block, you can use the retry statement to run the try block again. The following example demonstrates throwing an exception inside a database transaction block.

static void myJob(Args _args)
{
MyTable myTable;
boolean state = true;
;
try
{
ttsbegin;
state = false;
update_recordset myTable setting
myField = "value"
where myTable.id == "001";
if(state==false)
{
throw error("Error text");
}
ttscommit;
}
catch(Exception::Error)
{
state = true;
retry;
}
catch
{
print "Unhandled Exception";
pause;
}
}



The throw statement throws an exception that causes the database transaction to halt and roll back. Code execution can’t continue inside the scope of the transaction, so the runtime ignores try and catch statements when inside a transaction. This means that an exception thrown inside a transaction can be caught only outside the transaction, as shown here.

static void myJob(Args _args)
{
try
{
ttsbegin;
try
{
...
throw error("Error text");
}
catch //Will never catch anything
{
}
ttscommit;
}
catch
{
print "Got it";
pause;
}
}


Although a throw statement takes the exception enumeration as a parameter, using the error method to throw errors is the best choice. The try statement’s catch list can contain more than one catch block. The first catch block in the preceding example catches error exceptions. The retry statement performs a jump to the first statement in the outer try block. The second catch block catches all exceptions not caught by catch blocks earlier in the try statement’s catch list. Table 8 describes the Dynamics AX system Exception data type enumerations that can be used in try-catch statements.

Table 8. Exception Data Type Enumerations
ElementDescription
DeadlockThrown when a database transaction has deadlocked.
Error[*]Thrown when an unrecoverable application error occurs. A catch block should assume that all database transactions in a transaction block have been halted and rolled back.
InternalThrown when an unrecoverable internal error occurs.
BreakThrown when a user presses the Break key or Ctrl+C.
DDEerrorThrown when an error occurs in the use of a Dynamic Data Exchange (DDE) system class.
SequenceThrown by the Dynamics AX kernel if a database error or database operation error occurs.
NumericThrown when an unrecoverable error occurs in the str2int, str2int64, or str2num system function.
CLRErrorThrown when an unrecoverable error occurs in a CLR process.
CodeAccessSecurityThrown when an unrecoverable error occurs in the demand method of a CodeAccessPermission object.
UpdateConflictThrown when an update conflict error occurs in a transaction block using optimistic concurrency control. The catch block should use a retry statement to attempt to commit the halted transaction.
UpdateConflictNotRecoveredThrown when an unrecoverable error occurs in a transaction block using optimistic concurrency control. The catch block shouldn’t use a retry statement to attempt to commit the halted transaction.
DuplicateKeyExceptionThrown when a duplicate key error occurs during an insert operation. The catch block should change the value of the primary keys and use a retry statement to attempt to commit the halted transaction.
DuplicateKeyExceptionNotRecoveredThrown when an unrecoverable duplicate key error occurs during an insert operation. The catch block shouldn’t use a retry statement to attempt to commit the halted transaction.

[*] The error method is a static method of the global X++ class for which the X++ compiler allows an abbreviated syntax. The expression Global::error(“Error text”) is equivalent to the error expression in the code examples earlier in this section. Don’t confuse these global X++ methods with Dynamics AX system API methods, such as newguid.

Dynamics AX 2009 introduces DuplicateKeyException. UpdateConflictException and now DuplicateKeyException are the only data exceptions that a Dynamics AX application can handle. Specifically, with DuplicateKeyException, the database transaction isn’t rolled back and the application is given a chance to recover. DuplicateKeyException facilitates application scenarios (such as Master Planning) that perform batch processing and handles duplicate key exceptions without aborting the transaction in the midst of the resource-intensive processing operation.

The following example illustrates the usage of DuplicateKeyException.

static void DuplicateKeyExceptionExample(Args _args)
{
MyTable myTable;

ttsbegin;
myTable.Name = 'Microsoft Dynamics AX';
myTable.insert();
ttscommit;

ttsbegin;
try
{
myTable.Name = 'Microsoft Dynamics AX';
myTable.insert();
}
catch(Exception::DuplicateKeyException)
{
info(strfmt('Transaction level: %1', appl.ttsLevel()));
info(strfmt('%1 already exists.', myTable.Name));
info(strfmt('Continuing insertion of other records'));
}
ttscommit;
}


In the preceding example, the catch block handles the duplicate key exception. Notice that the ttslevel (transaction level) is still 1, indicating that the transaction hasn’t aborted and the application can continue processing other records.

Interoperability

The X++ language has statements that allow interoperability (interop) with .NET CLR assemblies and COM components. The Dynamics AX runtime achieves this interoperability by providing Dynamics AX object wrappers around external objects and by dispatching method calls from the Dynamics AX object to the wrapped object.

 
Others
 
- Microsoft Dynamic AX 2009 : Syntax (part 2) - Statements - Data-Aware Statements
- Microsoft Dynamic AX 2009 : Syntax (part 1) - Variable Declarations & Expressions
- Microsoft Dynamic AX 2009 : Jobs & The Type System
- Installing Active Directory Domain Services (part 3) - Creating a Domain Controller
- Installing Active Directory Domain Services (part 2) - Components of an Active Directory Infrastructure & Adding the AD DS Role Using the Windows Interface
- Installing Active Directory Domain Services (part 1) - Active Directory, Identity and Access
- Microsoft Dynamic GP 2010 : Speeding up access to information with SmartList Favorites
- Microsoft Dynamic GP 2010 : Sorting data to get the information you want
- Understanding the Basics of Collaboration in SharePoint 2010 (part 2) - Editing Features in SharePoint 2010
- Understanding the Basics of Collaboration in SharePoint 2010 (part 1) - Using SharePoint Sites and Templates
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us