Throws or try-catch

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

    Here's the way I use it:

    Throws:

    • You just want the code to stop when an error occurs.
    • Good with methods that are prone to errors if certain prerequisites are not met.

    Try-Catch:

    • When you want to have the program behave differently with different errors.
    • Great if you want to provide meaningful errors to end users.

    I know a lot of people who always use Throws because it's cleaner, but there's just not nearly as much control.

提交回复
热议问题