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.


Contents

[edit] Try and Catch

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

try
{
   // Block of code that may fail
}
catch (exception::error)
{
   // Do something about this error
}

[edit] Retry

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.


[edit] Catch enums

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

[edit] Example

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;
}
Personal tools