Sample23: Structured Exception Handling (SEH)

This article describes the use of Structured Exception Handling (SEH) on the basis of five variants.

Download

Here you can access the source code for this sample.

1. Unpack the downloaded ZIP file.
2. Using a Visual Studio with TwinCAT installed, open the project via Open Project ....
3. Configure signing for this project by switching on TwinCAT signing with a right-click on Project->Properties->Tc Sign and configure your certificate and password if necessary.
For more information on signing C++ projects, click here.
4. Select your target system.
5. Build the sample (e.g. Build->Build Solution).
6. Activate the configuration by clicking on Sample23: Structured Exception Handling (SEH) 1:.
The sample is ready for operation.

Description

The sample contains five variants that demonstrate the use of SEH in TwinCAT C++:

  1. Exception in the case of a NULL-pointer access
  2. Exception in the case of a NULL-pointer access with a filter
  3. Exception with Finally
  4. A customer-specific structured exception
  5. Exception with Continue block

All of these variants can be selected via a drop-down box at the instance of the C++:
Sample23: Structured Exception Handling (SEH) 2:

After selecting a variant you can also write the value at runtime by right-clicking on the first column:
Sample23: Structured Exception Handling (SEH) 3:

All variants write trace messages to illustrate the behavior, so that messages appear in TwinCAT Engineering:

Sample23: Structured Exception Handling (SEH) 4:

Understanding the sample

The selection in the drop-down box is an enumeration that is used in the CycleUpdate() of the module for selecting a case (switch case). As a result the variants can be considered independently of one another here:

  1. Exception in the case of a NULL-pointer access
    Here, a PBYTE is created as NULL and used afterwards, which leads to an exception.
    This is intercepted by the TcTry{} block and an output generated.
  2. Exception in the case of a NULL-pointer access with a filter
    This variant also accesses a NULL-pointer, but in TcExcept{} it uses a method, FilterException(), that is also defined in the module. Reactions take place to different exceptions within the method; in this case a message is merely output.
  3. Exception with Finally
    A NULL-pointer access takes place once again here, but this time a TcFinally{} block is executed in every case.
  4. A customer-specific structured exception
    By means of TcRaiseException() an exception is generated that is intercepted and processed by the FilterException() method. Since this is an exception defined in the module, the FilterException() method additionally outputs a further (specific) message.
  5. Exception with Continue block
    Here too, a NULL-pointer access takes place once again with TcExcept{}; however, this time the exception is forwarded after handling in the FilterException() method so that the further TcExcept{} also handles the exception.