Try-catch statements are used for exception handling in X++. A try-catch statement consists of two or more separate blocks of code, a try block which attempts some operation, and one or more catch blocks where exceptions thrown within the try block are caught. Exceptions may either be thrown by the kernel or by using the command throw from code within the try block. The catch part takes action on exceptions. You may have multiple catch blocks, each designed to catch a particular type of exception.
Action will only be taken on types of exceptions explicitly referenced in your code. The try-catch example shown will take action on the exception type's error and warning.
Action will only be taken on types of exceptions explicitly referenced in your code. The try-catch example shown will take action on the exception type's error and warning.
static void Intro_TryCatch(Args _args)
{
Counter counter;
try
{
while (counter < 10)
{
counter++;
if (counter MOD 7 == 0)
throw error("Counter MOD 7 is zero");
if (counter MOD 3 == 0)
throw warning("Counter MOD 3 is zero");
}
}
catch (Exception::Error)
{
print ( strfmt("An error appeared at loop %1", counter));
}
catch (Exception::Warning)
{
print ( strfmt("A warning appeared at loop %1", counter));
retry;
}
pause;
}