Throws or try-catch

后端 未结 10 1370
别跟我提以往
别跟我提以往 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:45

    • catch an exception only if you can handle it in a meaningful way
    • declare throwing the exception upward if it is to be handled by the consumer of the current method
    • throw exceptions if they are caused by the input parameters (but these are more often unchecked)

提交回复
热议问题