Handling common parts of exceptions together

前端 未结 3 555
心在旅途
心在旅途 2021-01-18 19:47

I currently have some code I am trying to refactor. A large set of exceptions has some common code for all exceptions as well as some specific code which needs to be handled

3条回答
  •  囚心锁ツ
    2021-01-18 20:48

    In general, try/catch should only appear sparsely in the code. The problem is that many times the actions done in a catch clause should also be done in case of early return, for example.

    Idiomatic C++ uses RAII extensively to avoid situation where you need to cleanup in a catch clause, which generally removes most of the work.

    Now, your pattern is not so bad per se, it does factor the common stuff. But this common stuff could perhaps be handled automatically.

    In all the code bases I have stumbled upon, only a few times did I see a genuine use of catch clause, don't use it as a clutch.

提交回复
热议问题