F_RaiseException
Die Funktion ermöglicht es durch Aufruf eine Exception mit einem bestimmten Exception Code zu werfen. Die Funktion sollte nur innerhalb eines __TRY-Blocks verwendet werden, damit das SPS-Programm nicht anhält.
![]() | Grundlegende Information zu __TRY, __CATCH, __FINALLY, __ENDTRY Siehe die Dokumentation zu __TRY, __CATCH, __FINALLY, __ENDTRY, um ein grundlegendes Verständnis vom Exception-Handling zu erlangen. |
FUNCTION F_RaiseException
Eingänge
VAR_INPUT
ExceptionCode : UDINT;
END_VAR
Name | Typ | Beschreibung |
---|---|---|
ExceptionCode | UDINT | Angabe des Exception Code. Siehe __SYSTEM.ExceptionCode |
Beispiel
PROGRAM MAIN
VAR
nCounter1 : INT;
nCounter2 : INT;
nCounter_TRY1 : INT;
nCounter_TRY2 : INT;
nCounter_CATCH : INT;
aData : ARRAY[1..cMax] OF INT;
nIndex : UINT;
exc : __SYSTEM.ExceptionCode;
lastExc : __SYSTEM.ExceptionCode;
END_VAR
VAR CONSTANT
cMax : UINT := 10;
END_VAR
// Counter 1
nCounter1 := nCounter1 + 1;
nIndex := nIndex + 1;
// TRY-CATCH block
__TRY
nCounter_TRY1 := nCounter_TRY1 + 1;
IF nIndex > cMax THEN
F_RaiseException(__SYSTEM.ExceptionCode.RTSEXCPT_ARRAYBOUNDS);
END_IF
nCounter_TRY2 := nCounter_TRY2 + 1;
__CATCH(exc)
nCounter_CATCH := nCounter_CATCH + 1;
// Exception logging
lastExc := exc;
// Correct the faulty variable values
IF (exc = __SYSTEM.ExceptionCode.RTSEXCPT_ARRAYBOUNDS) AND (nIndex > cMax) THEN
nIndex := cMax;
END_IF
__ENDTRY
aData[nIndex] := 123;
// Counter 2
nCounter2 := nCounter2 + 1;
Beispiel mit benutzerspezifischem Exception Code
Eigene benutzerspezifische Exception Codes sollten vor Verwendung mit RTSEXCPT_VENDOR_EXCEPTION_BASE verodert werden.
PROGRAM MAIN
VAR
nCounter1 : INT;
nCounter2 : INT;
nCounter_TRY1 : INT;
nCounter_TRY2 : INT;
nCounter_CATCH : INT;
aData : ARRAY[1..cMax] OF INT;
nIndex : UINT;
exc : __SYSTEM.ExceptionCode;
lastExc : __SYSTEM.ExceptionCode;
END_VAR
VAR CONSTANT
cMyOwnExceptionCode : UDINT := 123;
cMax : UINT := 10;
END_VAR
// Counter 1
nCounter1 := nCounter1 + 1;
nIndex := nIndex + 1;
// TRY-CATCH block
__TRY
nCounter_TRY1 := nCounter_TRY1 + 1;
IF nIndex > cMax THEN
F_RaiseException(__SYSTEM.ExceptionCode.RTSEXCPT_VENDOR_EXCEPTION_BASE OR cMyOwnExceptionCode);
END_IF
nCounter_TRY2 := nCounter_TRY2 + 1;
__CATCH(exc)
nCounter_CATCH := nCounter_CATCH + 1;
// Exception logging
lastExc := exc;
// Correct the faulty variable values
IF (exc = __SYSTEM.ExceptionCode.RTSEXCPT_VENDOR_EXCEPTION_BASE OR cMyOwnExceptionCode) AND (nIndex > cMax) THEN
nIndex := cMax;
END_IF
__ENDTRY
aData[nIndex] := 123;
// Counter 2
nCounter2 := nCounter2 + 1;
Entwicklungsumgebung | Zielplattform | Einzubindende SPS-Bibliotheken (Kategoriegruppe) |
---|---|---|
TwinCAT v3.1.4026.15 | PC oder CX (x86, x64, Arm®) | Tc2_System (System) >= 3.8.1.0 |