F_RaiseException

Calling the function makes it possible to throw an exception with a specific exception code. The function should only be used within a __TRY block so that the PLC program does not stop.

F_RaiseException 1:

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

F_RaiseException 2: Inputs

VAR_INPUT
    ExceptionCode : UDINT;
END_VAR

Name

Type

Description

ExceptionCode

UDINT

Specification of the ExceptionCode. See __SYSTEM.ExceptionCode

Sample

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;

Sample with user-specific ExceptionCode

User-specific ExceptionCodes should be ORed 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