You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Prefer custom exceptions to regular runtime_errors #2247
Feature request - Prefer custom exceptions compared to bulky if/else statements with std::runtime_error
Feature description
There are a few TODOs in the repository that are marked similar to:
TODO(jacobperron): use custom exception
They can found by grepping for that format, like here where they are often surrounded by bulky if/else statements that have already been fixed to look better like this. There are most likely more of these style statements in the code base, that just haven't been marked, for instance the NodeWaitables example was recognized when the original pull request was made in 2018, but these were the easiest examples to target and start with.
Implementation considerations
I guess the main question is what the original authors meant by "custom" because in the component_manager example there was a ComponentManagerException class:
class ComponentManagerException : public std::runtime_error
{
public:
explicit ComponentManagerException(const std::string & error_desc)
: std::runtime_error(error_desc) {}
};
It seems that it's a needless wrapper for std::runtime_error unless there's a benefit in passing the description via const reference. Would each of these exception follow a similar format, could a new general class be made, or can we just stick with runtime_error?
It seems that it's a needless wrapper for std::runtime_error unless there's a benefit in passing the description via const reference.
My understanding is that it is not a needless wrapper. In particular, what it allows you to do is write code that catches rclcpp ComponentManager exceptions, but not all std::runtime_error exceptions. Something like:
Ok, I have also seen other classes with their own, little Exception handler class. In that case, can I open a PR that deals with all the current cases:
TODO(person) use custom exception
To get their own Exception classes. Then, if needed deal with other areas that could maybe also use this style?
In that case, can I open a PR that deals with all the current cases:
I'm going to suggest that you don't open a single PR that deals with all of them. That is going to be difficult to review and get merged, since we have to inspect both the throw site as well as the handling stack. Instead, I'll suggest you open a PR on a case-by-case basis, and we can examine each of them to make sure that is the thing we want to do.
Activity
clalancette commentedon Jul 31, 2023
My understanding is that it is not a needless wrapper. In particular, what it allows you to do is write code that catches rclcpp ComponentManager exceptions, but not all
std::runtime_errorexceptions. Something like:CursedRock17 commentedon Aug 1, 2023
Ok, I have also seen other classes with their own, little Exception handler class. In that case, can I open a PR that deals with all the current cases:
To get their own Exception classes. Then, if needed deal with other areas that could maybe also use this style?
clalancette commentedon Aug 1, 2023
I'm going to suggest that you don't open a single PR that deals with all of them. That is going to be difficult to review and get merged, since we have to inspect both the
throwsite as well as the handling stack. Instead, I'll suggest you open a PR on a case-by-case basis, and we can examine each of them to make sure that is the thing we want to do.