Operators ‘__TRY’, ‘__CATCH’, ‘__FINALLY’, ‘__ENDTRY’

These operators are extended from the IEC 61131-3 standard and they are used for specific exception handling in IEC code.

Syntax

__TRY
    <statements_try>
__CATCH(exec)
    <statements_catch>
__FINALLY
    <statements_finally>
__ENDTRY
    <statements_next>

When a statement in the __Try operator throws an exception, the application does not stop. Instead, the application executes the statements in __Catch, starts the exception handling, and then executes the statements in __FINALLY. The exception handling ends with __ENDTRY, and the application executes the subsequent statements.

An IEC variable for an exception has the data type __System.ExceptionCode.

Example

If the statement in __TRY throws an exception, then program execution is not stopped. Instead, the statement in __CATCH is executed. Therefore, in this example, the application executes the exc function, then the statement in __FINALLY, and finally the statement in __ENDTRY.

FUNCTION Tester : UDINT
VAR_INPUT
    count : UDINT;
END_VAR
VAR_OUTPUT
    strExceptionText : STRING;
END_VAR
VAR
    exc : __SYSTEM.ExceptionCode;
END_VAR

__TRY
Tester := tryFun(count := count, testcase := g_testcase);  // This statement is tested. If it throws an exception, then the statement in __CATCH is executed first, and then the statement in __FINALLY.
__CATCH(exc)
HandleException(exc, strExceptionText => strExceptionText);
__FINALLY
GVL.g_count := GVL.g_count + 2;
__ENDTRY

See also

Data Type ‘__System.ExceptionCode’

TYPE ExceptionCode :
(
RTSEXCPT_UNKNOWN  := 16#FFFFFFFF,
RTSEXCPT_NOEXCEPTION  := 16#00000000,
RTSEXCPT_WATCHDOG  := 16#00000010,
RTSEXCPT_HARDWAREWATCHDOG  := 16#00000011,
RTSEXCPT_IO_CONFIG_ERROR  := 16#00000012,
RTSEXCPT_PROGRAMCHECKSUM  := 16#00000013,
RTSEXCPT_FIELDBUS_ERROR  := 16#00000014,
RTSEXCPT_IOUPDATE_ERROR  := 16#00000015,
RTSEXCPT_CYCLE_TIME_EXCEED  := 16#00000016,
RTSEXCPT_ONLCHANGE_PROGRAM_EXCEEDED  := 16#00000017,
RTSEXCPT_UNRESOLVED_EXTREFS   := 16#00000018,
RTSEXCPT_DOWNLOAD_REJECTED                := 16#00000019,
RTSEXCPT_BOOTPROJECT_REJECTED_DUE_RETAIN_ERROR        := 16#0000001A,
RTSEXCPT_LOADBOOTPROJECT_FAILED            := 16#0000001B,
RTSEXCPT_OUT_OF_MEMORY                    := 16#0000001C,
RTSEXCPT_RETAIN_MEMORY_ERROR            := 16#0000001D,
RTSEXCPT_BOOTPROJECT_CRASH                := 16#0000001E,
RTSEXCPT_BOOTPROJECTTARGETMISMATCH        := 16#00000021,
RTSEXCPT_SCHEDULEERROR                    := 16#00000022,
RTSEXCPT_FILE_CHECKSUM_ERR                := 16#00000023,
RTSEXCPT_RETAIN_IDENTITY_MISMATCH        := 16#00000024,
RTSEXCPT_IEC_TASK_CONFIG_ERROR            := 16#00000025,
RTSEXCPT_APP_TARGET_MISMATCH            := 16#00000026,
RTSEXCPT_ILLEGAL_INSTRUCTION            := 16#00000050,
RTSEXCPT_ACCESS_VIOLATION                := 16#00000051,
RTSEXCPT_PRIV_INSTRUCTION                := 16#00000052,
RTSEXCPT_IN_PAGE_ERROR                    := 16#00000053,
RTSEXCPT_STACK_OVERFLOW                    := 16#00000054,
RTSEXCPT_INVALID_DISPOSITION            := 16#00000055,
RTSEXCPT_INVALID_HANDLE                    := 16#00000056,
RTSEXCPT_GUARD_PAGE                        := 16#00000057,
RTSEXCPT_DOUBLE_FAULT                    := 16#00000058,
RTSEXCPT_INVALID_OPCODE                    := 16#00000059,
RTSEXCPT_MISALIGNMENT                    := 16#00000100,
RTSEXCPT_ARRAYBOUNDS                    := 16#00000101,
RTSEXCPT_DIVIDEBYZERO                    := 16#00000102,
RTSEXCPT_OVERFLOW                        := 16#00000103,
RTSEXCPT_NONCONTINUABLE                    := 16#00000104,
RTSEXCPT_PROCESSORLOAD_WATCHDOG            := 16#00000105,
RTSEXCPT_FPU_ERROR                        := 16#00000150,
RTSEXCPT_FPU_DENORMAL_OPERAND            := 16#00000151,
RTSEXCPT_FPU_DIVIDEBYZERO                := 16#00000152,
RTSEXCPT_FPU_INEXACT_RESULT                := 16#00000153,
RTSEXCPT_FPU_INVALID_OPERATION            := 16#00000154,
RTSEXCPT_FPU_OVERFLOW                    := 16#00000155,
RTSEXCPT_FPU_STACK_CHECK                := 16#00000156,
RTSEXCPT_FPU_UNDERFLOW                    := 16#00000157,
RTSEXCPT_VENDOR_EXCEPTION_BASE            := 16#00002000
RTSEXCPT_USER_EXCEPTION_BASE            := 16#00010000
) UDINT ;
END_TYPE