Skip to content

Prefer custom exceptions to regular runtime_errors #2247

Closed

Description

@CursedRock17

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?

Activity

  1. clalancette commented on Jul 31, 2023

    @clalancette
    Contributor

    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:

    try {
        component_manager->do_something();
    } catch (const rclcpp::ComponentManagerException & cme) {
        rclcpp_component_manager_error_handling();
    } catch (const std::runtime_error & e) {
        general_error_handling();
    }
    
  2. CursedRock17 commented on Aug 1, 2023

    @CursedRock17
    ContributorAuthor

    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?

  3. clalancette commented on Aug 1, 2023

    @clalancette
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions