Handling common parts of exceptions together

前端 未结 3 564
心在旅途
心在旅途 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:38

    A large set of exceptions has some common code

    Move the common code into a function or method. Call the method from each catch. Just like eliminating any other duplication; the presence of a try..catch makes no difference.

    But if you are really concerned about "a large set of exceptions", the real problem might be that you have a large set of exceptions. Why do different exceptions require different handling at all? Are exceptions really being used for only exceptional events?

提交回复
热议问题