ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Raising Exceptions in Subscriber Callbacks

asked 2023-02-07 12:15:38 -0500

Rhea gravatar image

ROS distribution: noetic, rospy

I want to raise specific exceptions depending on the data received from the rostopic. However, raising exceptions in the subscriber callback (user_callback) returns a bad callback error due to rospy callback exception handling:

Is there a way to raise custom ROS exceptions in one node (user_command node) and handle it in another (control node)? Or is there another way to filter data from a subscribed topic and raise an exception accordingly?

For example: I am subscribed to /user_commands. If a "t" is published I want to immediately exit the "scan" state while loop and switch to another state (track)

class command_t(Exception):

class command_h(Exception):

class command_hv(Exception):

class actions:
    def __init__(self):
           #initialize all my variables

    def user_callback(self,msg):

       if ( == 't'):
           raise command_t #BAD CALLBACK ERROR
       if ( == 'h'):
           raise command_h

    def scan(self):
           rospy.loginfo("scan mode")
           local_delta = 0
           while (x0 + local_delta < x1):
               self.waypoint(x0 + local_delta, y0, z)
               self.waypoint(x0 + local_delta, y1, z)
               local_delta = local_delta +
               self.waypoint(x0 + local_delta, y1, z)
               self.waypoint(x0 + local_delta, y0 , z)
               local_delta = local_delta +
           local_delta = 0

       except command_t:
       except command_h:

if __name__ == '__main__':
    action = actions()
    user_sub = rospy.Subscriber("/user_commands", String, action.user_callback)

    rate = rospy.Rate(100)
edit retag flag offensive close merge delete


Regardless of the technical implementation, and probably pedantic, but using exceptions for regular control flow seems like an abuse of the concept to me.

If you're happy, we're happy of course, but you might be trying to solve an xy-problem, or have other options which would follow best-practices more and don't rely on (potentially) incomplete implementations.

gvdhoorn gravatar image gvdhoorn  ( 2023-02-08 06:53:45 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2023-02-08 02:50:40 -0500

You'd have to handle your own exceptions that are thrown so that the ROS internal system isn't catching them for you (or not catching them). The exceptions you throw and don't catch in callbacks are being processed on the node executor thread.

Instead, you could have the callback simply buffer the data into an internal variable which you can call a function to interpret and process on a timer or in another thread which you can throw and catch your own exceptions while processing them in a context which you can do something contextually about.

edit flag offensive delete link more

Question Tools



Asked: 2023-02-07 12:15:38 -0500

Seen: 140 times

Last updated: Feb 08 '23