From 0bfeeca11ad5db318e7da1b3da7faa720906a052 Mon Sep 17 00:00:00 2001 From: Iori Yanokura Date: Mon, 23 Dec 2024 15:33:49 +0900 Subject: [PATCH] Handle KeyboardInterrupt and SystemExit separately and log all other exceptions with stack trace --- ros/kxr_controller/python/kxr_controller/serial.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ros/kxr_controller/python/kxr_controller/serial.py b/ros/kxr_controller/python/kxr_controller/serial.py index 9e7e7982..d8000427 100644 --- a/ros/kxr_controller/python/kxr_controller/serial.py +++ b/ros/kxr_controller/python/kxr_controller/serial.py @@ -1,3 +1,5 @@ +import traceback + import rospy import serial @@ -33,6 +35,9 @@ def serial_call_with_retry(func, *args, max_retries=1, retry_interval=0.1, **kwa while attempts < max_retries: try: return func(*args, **kwargs) + except (KeyboardInterrupt, SystemExit): + # Re-raise these exceptions to allow program termination + raise except ( serial.SerialException, OSError, @@ -43,5 +48,11 @@ def serial_call_with_retry(func, *args, max_retries=1, retry_interval=0.1, **kwa rospy.logerr(f"[{func.__name__}] Error: {e}") attempts += 1 rospy.sleep(retry_interval) + except Exception as e: + # Catch all other exceptions and log them + rospy.logerr(f"[{func.__name__}] Exception occurred: {e}") + rospy.logerr(f"Stack trace:\n{traceback.format_exc()}") + attempts += 1 + rospy.sleep(retry_interval) rospy.logerr(f"[{func.__name__}] Failed after {max_retries} attempts.") return None