F_RaiseException
Calling the function makes it possible to throw a runtime exception with a specific exception code.
![]() | If you use the F_RaiseException() function outside a __TRY block, the exception is caught by TwinCAT Exception Handling and the execution of the control is stopped. |
![]() | Basic information on __TRY, __CATCH, __FINALLY, __ENDTRY See the documentation on __TRY, __CATCH, __FINALLY, __ENDTRY to gain a basic understanding of exception handling. |
FUNCTION F_RaiseException
Inputs
VAR_INPUT
ExceptionCode : UDINT;
END_VAR
Name | Type | Description |
---|---|---|
ExceptionCode | UDINT | Specification of the ExceptionCode. See __SYSTEM.ExceptionCode |
Example
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;
Example with user-specific ExceptionCode
User-specific Exception Codes should be OR-ed with RTSEXCPT_VENDOR_EXCEPTION_BASE before use.
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;
Development Environment | Target platform | PLC libraries to be integrated (category group) |
---|---|---|
TwinCAT v3.1.4026.15 | PC or CX (x86, x64, Arm®) | Tc2_System (system) >= 3.8.1.0 |