__TRY, __CATCH, __FINALLY, __ENDTRY

Définition

Ces opérateurs ne sont pas définis par la norme IEC 61131-3.

Les opérateurs __TRY, __CATCH, __FINALLY, __ENDTRY permettent de gérer des exceptions de manière spécifique dans le code IEC. Vous pouvez ainsi appliquer des instructions particulières en cas d'erreur. En outre, les exceptions n'entraînent pas l'arrêt du programme (comme habituellement).

Syntaxe

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

Fonction

<statements_catch> est exécuté en cas de détection d'une exception lors de l'exécution de <statements_try> (même dans les fonctions appelées par la suite). Le programme se poursuit, contrairement à la procédure habituelle lorsque d'autres exceptions sont détectées. Un message indiquant le décalage de plage et le type d'exception est consigné dans le système d'exécution.

S'il a été programmé, <statements_finally> est automatiquement exécuté après <statements_catch>, suivi de <statements_next>.

La variable <exception> doit être de type __SystemExceptionCode.

Exemple

Si l'instruction sous __TRY génère une exception, le programme se poursuit et les instructions sous __CATCH sont exécutées. La fonction exc est donc exécutée suivie de l'instruction sous __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 produces 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

Utilisez la commande Arrêter l'exécution aux exceptions traitées pour arrêter l'exécution là où l'erreur s'est produite, malgré la gestion programmée des exceptions.

Type __System.Exception

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