# What is the expected behavior of rclcpp in case of an exception raised in a user callback?

**URL:** <https://discourse.openrobotics.org/t/what-is-the-expected-behavior-of-rclcpp-in-case-of-an-exception-raised-in-a-user-callback/27527>\
**Category:** ROS General\
**Tags:** ros2\
**Created:** [September 27, 2022, 12:03pm UTC](https://discourse.openrobotics.org/t/what-is-the-expected-behavior-of-rclcpp-in-case-of-an-exception-raised-in-a-user-callback/27527 "2022-09-27T12:03:29Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![johannesmeyer](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/johannesmeyer/32/6625_2.png) [@johannesmeyer](https://discourse.openrobotics.org/u/johannesmeyer)\
**Post date:** [September 27, 2022, 12:03pm UTC](https://discourse.openrobotics.org/t/what-is-the-expected-behavior-of-rclcpp-in-case-of-an-exception-raised-in-a-user-callback/27527/1 "2022-09-27T12:03:29Z")

</div>

_Reposted from [Exception handling in user callbacks? by meyerj · Pull Request #2017 · ros2/rclcpp · GitHub](https://github.com/ros2/rclcpp/pull/2017)_

I tried to find some information about this topic in the [documentation](https://docs.ros.org/en/rolling/Concepts/About-Executors.html), in the [code](https://github.com/ros2/rclcpp), on [GitHub](https://github.com/search?q=rclcpp+callback+exception&type=issues), on [Discourse](http://discourse.ros.org/search?q=rclcpp%20callback%20exception), on [ROS Answers](https://answers.ros.org/questions/), but failed to find something conclusive, or maybe used the wrong search terms. Only [this post](http://discourse.ros.org/t/my-ros2-minimal-examples-difficulties-coming-from-ros1/18705/13) and [this answer](https://answers.ros.org/question/356549/how-to-handle-exceptions-in-the-user-defined-callback-in-service-server-and-how-to-inform-service-client-about-the-failed-service-call-in-ros2-rclcpp/) seem to be related. For the special case of service callbacks I remember having seen a discussion/feature request to forward exceptions to the caller as a special response like in ROS 1, but did not find it anymore now.

1. _User callbacks must never throw?_

2. _Did I miss a place where this is already handled within rclcpp?_

3. _Always catch exceptions when spinning?_

I can almost not believe that there is no foreseen or documented way to prevent that any minor fault terminates the whole process, or that this behavior is “by design”? I am sorry in case there is something more obvious, and I just missed it.

It is easy to reproduce the crash with the [`minimal_service` example](https://github.com/ros2/examples/blob/0410665143fd89b33e98b4c7d90829b32a6158f7/rclcpp/services/minimal_service/main.cpp) in [ros2/examples](https://github.com/ros2/examples), by adding a throw statement in the callback:

```sh
$ ros2 run examples_rclcpp_minimal_service service_main &
[1] 353822
$ ros2 service call /add_two_ints example_interfaces/srv/AddTwoInts "{}"
requester: making request: example_interfaces.srv.AddTwoInts_Request(a=0, b=0)

[INFO] [1663789664.837616992] [minimal_service]: request: 0 + 0
terminate called after throwing an instance of 'std::runtime_error'
  what(): some error
^C[1]+ Exit 250 ros2 run examples_rclcpp_minimal_service service_main
$ 

```

---

_[View the full topic](https://discourse.openrobotics.org/t/what-is-the-expected-behavior-of-rclcpp-in-case-of-an-exception-raised-in-a-user-callback/27527)._
