Throws or try-catch

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

    When to use what. I searched a lot about this. There is no hard and fast rule.

    "But As a developer, Checked exceptions must be included in a throws clause of the method. This is necessary for the compiler to know which exceptions to check. By convention, unchecked exceptions should not be included in a throws clause.
    Including them is considered to be poor programming practice. The compiler treats them as comments, and does no checking on them."

    Source : SCJP 6 book by Kathy Sierra

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • If the method where the exception got raised has a sufficent amount of information to deal with it then it should catch, generate useful information about what happened and what data was being processed.

    0 讨论(0)
  • 2020-11-28 03:42

    My personnal rule of thumb for that is simple :

    • Can I handle it in a meaningful way (added from comment)? So put code in try/catch. By handle it, I mean be able to inform the user/recover from error or, in a broader sense, be able to understand how this exception affects the execution of my code.
    • Elsewhere, throw it away

    Note : this replys is now a community wiki, feel free to add more info in.

    0 讨论(0)
  • 2020-11-28 03:42

    A method should only throws an exception if it can make reasonable guarantees surrounding the state of the object, any parameters passed to the method, and any other objects the method acts upon. For example, a method which is supposed to retrieve from a collection an item which the caller expects to be contained therein might throws a checked exception if the item which was expected to exist in the collection, doesn't. A caller which catches that exception should expect that the collection does not contain the item in question.

    Note that while Java will allow checked exceptions to bubble up through a method which is declared as throwing exceptions of the appropriate types, such usage should generally be considered an anti-pattern. Imagine, for example, that some method LookAtSky() is declared as calling FullMoonException, and is expected to throw it when the Moon is full; imagine further, that LookAtSky() calls ExamineJupiter(), which is also declared as throws FullMoonException. If a FullMoonException were thrown by ExamineJupiter(), and if LookAtSky() didn't catch it and either handle it or wrap it in some other exception type, the code which called LookAtSky would assume the exception was a result of Earth's moon being full; it would have no clue that one of Jupiter's moons might be the culprit.

    Exceptions which a caller may expect to handle (including essentially all checked exceptions) should only be allowed to percolate up through a method if the exception will mean the same thing to the method's caller as it meant to the called method. If code calls a method which is declared as throwing some checked exception, but the caller isn't expecting it to ever throw that exception in practice (e.g. because it thinks it pre-validated method arguments), the checked exception should be caught and wrapped in some unchecked exception type. If the caller isn't expecting the exception to be thrown, the caller can't be expecting it to have any particular meaning.

    0 讨论(0)
提交回复
热议问题