> For the complete documentation index, see [llms.txt](https://support.powerio.com/hub/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://support.powerio.com/hub/faq/faq-en/hvac-codesys-faq/automatischer-neustart-im-fehlerfall.md).

# Automatic restart in the event of an error

Enabling and disabling error handling in CODESYS to log information about an exception and perform a restart of the controller or the Runtime.

{% hint style="warning" %}
Please note that for full functionality of the function *OnException* **you need at least firmware version 3.2** .

The function *OnException* is preconfigured for powerIO devices and is recommended only for powerIO devices.
{% endhint %}

## How can the controller be restarted automatically in the event of an error?

The following article describes how an exception can be logged during runtime and how the Runtime or the device can be restarted in the exception state.

## Description

* That the controller, in the event of an Exception **without** **"emergency stop"** (no kernel panic) of the system, the message is logged and the CODESYS Runtime is restarted. This is the “automatic soft reset in case of an error”.
* That the controller, in the event of an Exception **with "emergency stop"** (kernel panic) of the system, the message *attempts* to log and the system is restarted.

  This is the “automatic hard reset in case of an error”

## Function description *OnExceptionWrapper*

In [Start project "Blank" ](/hub/downloads/en/codesys-hvac-templates/template-start.md)are located under the view *POU* the two functions

* *OnException* and
* *OnExceptionWrapper*

The function *OnExceptionWrapper*, calls the *OnException* function with the specified settings.

```iecst
OnException( 
	EventPrm:=EventPrm,
	xDebug := FALSE,
	xLogExceptionToFile:=TRUE,
	xRestartRuntimeOnException := TRUE,
	tExceptionFunctionTimeout := TIME#45S0MS
	);
```

## Activate/deactivate the exception handling function

1\. Click on "Task configuration "

![](https://ajeuwbhvhr.cloudimg.io/colony-recorder.s3.amazonaws.com/files/2023-11-21/a17783ad-3118-4508-870f-28e2ff3d4f84/user_cropped_screenshot.jpeg?tl_px=0,231\&br_px=859,712\&force_format=png\&width=860\&wat_scale=76\&wat=1\&wat_opacity=1\&wat_gravity=northwest\&wat_url=https://colony-recorder.s3.amazonaws.com/images/watermarks/77328A_standard.png\&wat_pad=122,212)

2\. Click on "System events"

![](https://ajeuwbhvhr.cloudimg.io/colony-recorder.s3.amazonaws.com/files/2023-11-21/17d6f9e5-ba67-49c0-a42f-e26e029ddea3/user_cropped_screenshot.jpeg?tl_px=274,0\&br_px=1134,480\&force_format=png\&width=860\&wat_scale=76\&wat=1\&wat_opacity=1\&wat_gravity=northwest\&wat_url=https://colony-recorder.s3.amazonaws.com/images/watermarks/77328A_standard.png\&wat_pad=402,107)

3\. Check whether the "Exception" function already exists.

![](https://ajeuwbhvhr.cloudimg.io/colony-recorder.s3.amazonaws.com/files/2023-11-21/959b345f-8448-402d-9740-889e366bafed/user_cropped_screenshot.jpeg?tl_px=195,0\&br_px=1055,480\&force_format=png\&width=860\&wat_scale=76\&wat=1\&wat_opacity=1\&wat_gravity=northwest\&wat_url=https://colony-recorder.s3.amazonaws.com/images/watermarks/77328A_standard.png\&wat_pad=402,173)

4\. If present, check whether it is "Active"

![](https://ajeuwbhvhr.cloudimg.io/colony-recorder.s3.amazonaws.com/files/2023-11-21/97f6a3fb-6992-46d4-b3f4-0048d7d99f92/screenshot.jpeg?tl_px=1905,0\&br_px=2765,480\&force_format=png\&width=860\&wat_scale=76\&wat=1\&wat_opacity=1\&wat_gravity=northwest\&wat_url=https://colony-recorder.s3.amazonaws.com/images/watermarks/77328A_standard.png\&wat_pad=402,177)

5\. If not active, then activate

![](https://ajeuwbhvhr.cloudimg.io/colony-recorder.s3.amazonaws.com/files/2023-11-21/6ed9e473-a7e9-40ab-a11b-3d5a7fb4ad65/screenshot.jpeg?tl_px=1905,0\&br_px=2765,480\&force_format=png\&width=860\&wat_scale=76\&wat=1\&wat_opacity=1\&wat_gravity=northwest\&wat_url=https://colony-recorder.s3.amazonaws.com/images/watermarks/77328A_standard.png\&wat_pad=402,177)

## Adding the exception handling function

If you want to add exception handling to an existing project, you can copy the two functions from the [Start project "Blank" ](/hub/downloads/en/codesys-hvac-templates/template-start.md)and add the event task yourself.

To do this, proceed as follows:

1\. Navigate to "Task configuration "

![](https://ajeuwbhvhr.cloudimg.io/colony-recorder.s3.amazonaws.com/files/2023-11-21/b299d8ef-01ff-4bdf-a9e5-60610b76ab6e/user_cropped_screenshot.jpeg?tl_px=0,237\&br_px=859,718\&force_format=png\&width=860\&wat_scale=76\&wat=1\&wat_opacity=1\&wat_gravity=northwest\&wat_url=https://colony-recorder.s3.amazonaws.com/images/watermarks/77328A_standard.png\&wat_pad=166,212)

2\. Open the "System events" tab

![](https://ajeuwbhvhr.cloudimg.io/colony-recorder.s3.amazonaws.com/files/2023-11-21/fabaa8e3-458d-4506-be77-36339e0ddeec/user_cropped_screenshot.jpeg?tl_px=285,0\&br_px=1145,480\&force_format=png\&width=860\&wat_scale=76\&wat=1\&wat_opacity=1\&wat_gravity=northwest\&wat_url=https://colony-recorder.s3.amazonaws.com/images/watermarks/77328A_standard.png\&wat_pad=402,105)

3\. Click "Add Event Handler"

![](https://ajeuwbhvhr.cloudimg.io/colony-recorder.s3.amazonaws.com/files/2023-11-21/b8682791-790f-4cad-b089-b1c1d0b2499a/user_cropped_screenshot.jpeg?tl_px=46,0\&br_px=905,480\&force_format=png\&width=860\&wat_scale=76\&wat=1\&wat_opacity=1\&wat_gravity=northwest\&wat_url=https://colony-recorder.s3.amazonaws.com/images/watermarks/77328A_standard.png\&wat_pad=402,131)

4\. Select event type "Exception"

![](https://ajeuwbhvhr.cloudimg.io/colony-recorder.s3.amazonaws.com/files/2023-11-21/430c91c1-d6d7-4602-83a4-690c952034cb/user_cropped_screenshot.jpeg?tl_px=693,315\&br_px=1553,796\&force_format=png\&width=860\&wat_scale=76\&wat=1\&wat_opacity=1\&wat_gravity=northwest\&wat_url=https://colony-recorder.s3.amazonaws.com/images/watermarks/77328A_standard.png\&wat_pad=402,212)

5\. Enter exception name "OnExceptionWrapper"

![](https://ajeuwbhvhr.cloudimg.io/colony-recorder.s3.amazonaws.com/files/2023-11-21/d08770c6-8fe3-4339-88e2-ded86e4caad0/user_cropped_screenshot.jpeg?tl_px=494,279\&br_px=1258,706\&force_format=png\&width=764)

6\. Click "POUs"

![](https://ajeuwbhvhr.cloudimg.io/colony-recorder.s3.amazonaws.com/files/2023-11-21/586e7dc6-2d8e-4b1d-8352-cbb134b6478f/user_cropped_screenshot.jpeg?tl_px=803,293\&br_px=1568,720\&force_format=png\&width=764\&wat_scale=68\&wat=1\&wat_opacity=1\&wat_gravity=northwest\&wat_url=https://colony-recorder.s3.amazonaws.com/images/watermarks/77328A_standard.png\&wat_pad=357,188)

7\. Click "OK"

![](https://ajeuwbhvhr.cloudimg.io/colony-recorder.s3.amazonaws.com/files/2023-11-21/b407b030-9d13-48a1-9c06-bf8ba6c15cb6/user_cropped_screenshot.jpeg?tl_px=542,395\&br_px=1402,876\&force_format=png\&width=860\&wat_scale=76\&wat=1\&wat_opacity=1\&wat_gravity=northwest\&wat_url=https://colony-recorder.s3.amazonaws.com/images/watermarks/77328A_standard.png\&wat_pad=402,212)

8\. Switch the view to "POUs"

![](https://ajeuwbhvhr.cloudimg.io/colony-recorder.s3.amazonaws.com/files/2023-11-21/a0288e24-8490-45c5-a66b-b3926deb77a8/user_cropped_screenshot.jpeg?tl_px=0,1211\&br_px=859,1692\&force_format=png\&width=860\&wat_scale=76\&wat=1\&wat_opacity=1\&wat_gravity=northwest\&wat_url=https://colony-recorder.s3.amazonaws.com/images/watermarks/77328A_standard.png\&wat_pad=73,377)

9\. Open function "OnExceptionWrapper (FUN)"

![](https://ajeuwbhvhr.cloudimg.io/colony-recorder.s3.amazonaws.com/files/2023-11-21/90abd384-bc64-430d-8ada-452f8c7e72d6/user_cropped_screenshot.jpeg?tl_px=0,20\&br_px=859,501\&force_format=png\&width=860\&wat_scale=76\&wat=1\&wat_opacity=1\&wat_gravity=northwest\&wat_url=https://colony-recorder.s3.amazonaws.com/images/watermarks/77328A_standard.png\&wat_pad=143,212)

10\. Configure "OnException" function

![](https://ajeuwbhvhr.cloudimg.io/colony-recorder.s3.amazonaws.com/files/2023-11-21/a1ce65ed-e2d1-42b2-8a01-601add236224/user_cropped_screenshot.jpeg?tl_px=325,427\&br_px=1185,908\&force_format=png\&width=860\&wat_scale=76\&wat=1\&wat_opacity=1\&wat_gravity=northwest\&wat_url=https://colony-recorder.s3.amazonaws.com/images/watermarks/77328A_standard.png\&wat_pad=402,212)

## Exception log not available as a file

If after an Exception the file `codesysexception.log` under **Files** is missing, check the setup of the function *OnException*. Without this file, the exception code and task are missing, because the CODESYS log does not contain an entry for the exception after an automatic restart.

Prerequisite: The project is open in CODESYS.

{% stepper %}
{% step %}

### Check Event Handler

Open **Task configuration** > **System events**. The event **Exception** calls the function `OnExceptionWrapper` and is active.
{% endstep %}

{% step %}

### Enable logging

Open in the view **POUs** the function `OnExceptionWrapper`. Set `xLogExceptionToFile := TRUE`.
{% endstep %}

{% step %}

### Check log file path

Open in the view **POUs** the function `OnException`. If `sExceptionLogPath` contains an absolute path like `/tmp/codesysexception.log` set the relative path:

```iecst
sExceptionLogPath : STRING(32) := 'codesysexception.log';
```

The Runtime blocks absolute paths and then does not write a file.
{% endstep %}

{% step %}

### Load project

Load the modified project onto the device. The boot project then contains the modified function.
{% endstep %}

{% step %}

### Save exception log

After the next Exception, copy the file `codesysexception.log` from the tab **Files** to the PC.
{% endstep %}
{% endstepper %}

The meaning of the exception codes and how to save the CODESYS log without a file are described under [Where is the CODESYS log located on powerIO devices?](https://github.com/powerIO-GmbH/support-hub/tree/main/space-faq/de/hvac-codesys-faq/wo-liegt-das-codesys-log.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://support.powerio.com/hub/faq/faq-en/hvac-codesys-faq/automatischer-neustart-im-fehlerfall.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
