Axaptapedia is now maintained by AgileCadence For more information please click here.

Exception handling

From Axaptapedia
Jump to: navigation, search

When execution a string of code, there is a risk that something may go wrong. Exception handling lets you handle system errors by your own code.


Try and Catch[edit]

If you are able to predict a block of code, where an exception will occur under certain circumstances, you can use the try command:

<xpp> try {

  // Block of code that may fail

} catch (exception::error) {

  // Do something about this error

} </xpp>

Retry[edit]

If you enter Retry in the catch block the try block is executes once more, but be carefull as this can result in a never-ending loop.


Catch enums[edit]

Enum (exception::) Description
Info
Warning
Deadlock
Error
Internal
Break
DDEerror
Sequence
Numeric
CLRError
CodeAccessSecurity
UpdateConflict
UpdateConflictNotRecovered

Example[edit]

<xpp> static void TryCatchTest(Args _args) {

   Map m = new Map(types::Integer, types::String);
   str s;
   int i;
   ;
   m.insert(0,"Zero");
   m.insert(1, "First");
   m.insert(2, "Second");
   m.insert(4, "Fourth");


   for (i=0;i<4;i++)
   {
       try
       {
           s = m.lookup(i);
           print s;
       }
       catch (exception::Error)
       {
           i++;    // Try to solve the problem (This IS just an example!)
           retry;  // retry the statement. 
       }
   }
   pause;

} </xpp>