Datentyp __SYSTEM.ExceptionCode

Eine IEC-Variable für eine Exception hat den Datentyp __SYSTEM.ExceptionCode. Eine solche Exception wird beispielsweise vom __TRY, __CATCH, __FINALLY, __ENDTRY gefangen, welches einem gezieltem Exception-Handling dient.

Zudem können Sie den Datentyp ExceptionCode verwenden, um aufgetretene Fehler oder Ausnahmen im Anwendungscode selbst zu klassifizieren und anschließend beispielsweise zu loggen. Ein Beispiel dazu finden Sie unten auf dieser Seite.

Darüber hinaus enthält die Tc2_System-Bibliothek die Funktion F_RaiseException, die das Werfen eines bestimmten Exception Codes ermöglicht.

Datentyp __SYSTEM.ExceptionCode 1:

Wenn Sie die Funktion F_RaiseException() außerhalb eines __TRY-Blocks verwenden, wird die Exception vom TwinCAT Exception Handling gefangen und die Ausführung der Steuerung gestoppt.

Datentyp  __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

Implementierungsbeispiel zur Klassifizierung von Fehlern/Ausnahmen

Globale Variablenliste „GVL_Exc“:

VAR_GLOBAL
    nCheckBounds              : INT;
    nCheckBounds_OutOfBounds  : INT;
    myException               : __SYSTEM.ExceptionCode;
END_VAR

Funktion „CheckBounds“:

// Implicitly generated code : DO NOT EDIT
FUNCTION CheckBounds : DINT
VAR_INPUT
    index, lower, upper       : DINT;
END_VAR
// Only an implementation suggestion
{noflow}
GVL_Exc.nCheckBounds := GVL_Exc.nCheckBounds + 1;

// Index too low
IF index < lower THEN
    CheckBounds                      := lower;
 
    // Option 1: Log exception code in global exception variable
    GVL_Exc.myException              := __SYSTEM.ExceptionCode.RTSEXCPT_ARRAYBOUNDS;
 
    // Option 2: Increment global "out of bounds" counter
    GVL_Exc.nCheckBounds_OutOfBounds := GVL_Exc.nCheckBounds_OutOfBounds + 1;
 
    // Option 3: Raise exception
    // Please note: To avoid a runtime exception and thus a runtime stop, F_RaiseException could be used within __TRY / __CATCH.
    F_RaiseException(__SYSTEM.ExceptionCode.RTSEXCPT_ARRAYBOUNDS);
 
// Index too high
ELSIF index > upper THEN
    CheckBounds                      := upper;
 
    // Option 1: Log exception code in global exception variable
    GVL_Exc.myException              := __SYSTEM.ExceptionCode.RTSEXCPT_ARRAYBOUNDS;
 
    // Option 2: Increment global "out of bounds" counter
    GVL_Exc.nCheckBounds_OutOfBounds := GVL_Exc.nCheckBounds_OutOfBounds + 1;
 
    // Option 3: Raise exception
    // Please note: To avoid a runtime exception and thus a runtime stop, F_RaiseException could be used within __TRY / __CATCH.
    F_RaiseException(__SYSTEM.ExceptionCode.RTSEXCPT_ARRAYBOUNDS);
 
// Index OK
ELSE
    CheckBounds := index;
END_IF

Siehe auch: