Where do you like to catch exceptions and why?

前端 未结 9 1487
别跟我提以往
别跟我提以往 2020-12-03 08:31

Where do you like to catch exceptions and why?

I\'m interested in seeing where people find it useful to put their try/catch blocks in the hope that some general patt

9条回答
  •  日久生厌
    2020-12-03 09:25

    I like to catch around handlers in the controller that handle events fired from the view. If exceptions give the strong guarantee of safety this is a useful catch point because it is high level enough to report the error and the handlers are usually atomic and related to something that the user has just done so hopefully they'll be able to work out what's going on.

    void Controller::on_edit_entity( const Entity& entity ) {
        try {
            Command::ptr command = new EditEntityCommand( entity );
            push_command( command );
        }
        catch ( const std::exception& exception ) {
            fprintf( stderr, "%s.\n", exception.what() );
        }
    }
    

提交回复
热议问题