Throws or try-catch

后端 未结 10 1360
别跟我提以往
别跟我提以往 2020-11-28 03:15

What is the general rule of thumb when deciding whether to add a throws clause to a method or using a try-catch?

From what I\'ve read mysel

10条回答
  •  温柔的废话
    2020-11-28 03:40

    The decision to add a try-catch or a throws clause to your methods depends on "how you want (or have) to handle your exception".

    How to handle an exception is a wide and far from trivial question to answer. It involves specially the decision of where to handle the exception and what actions to implement within the catch block. In fact, how to handle an exception should be a global design decision.

    So answering your questions, there is no rule of thumb.

    You have to decide where you want to handle your exception and that decision is usually very specific to your domain and application requirements.

提交回复
热议问题