Throws or attempt to catch

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

From what I read myself, throws should be used when the caller violated its end of the contract (the passed object), and try-catch should be used when an exception occurs during an operation performed inside the method. It is right? If so, what should be done on the caller side?

PS: I searched through Google and SO, but would like a clear answer to this question.

+63
java exception exception-handling try-catch throws
Jul 08 '10 at 12:01
source share
10 answers
  • catch the exception only if you can handle it in a meaningful way
  • declare to throw an exception up if it should be handled by the consumer of the current method.
  • exclude exceptions if they are caused by input parameters (but they are most often not marked)
+46
Jul 08 '10 at 12:05
source share

In general, a method should throw an exception to the caller when it cannot handle the associated problem locally. For example, if a method is to read from a file with a given path, IOExceptions cannot be processed in a locally reasonable way. The same goes for invalid input, adding that my personal choice is to throw an unchecked exception, such as IllegalArgumentException in this case.

And he should catch an exception from the called method if:

  • this is something that can be processed locally (for example, trying to convert the input string to a number, and if the conversion fails, it is entirely acceptable to return the default value instead),
  • or it should not be thrown (for example, if an exception comes from the lower level, specific to the implementation, implementation details of which should not be visible to the caller - for example, I do not want to show that my DAO uses Hibernate to save my entities, so I catch everything HibernateExceptions locally and convert them to my own exception types).
+13
Jul 08 '10 at 12:05
source share

My personal rule for this is simple:

  • Can I handle it in a meaningful way (added from a comment)? So enter the code in try/catch . By its processing, I mean informing the user / recovering due to an error or, in a broader sense, understanding how this exception affects the execution of my code.
  • Throw it away elsewhere.

Note: this answer is now a wiki community, feel free to add more information.

+9
Jul 08 2018-10-12T00:
source share

Here is how I use it:

Throws:

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

Try-catch:

  • If you want to have a program behave differently with different errors.
  • Great if you want to provide meaningful errors for end users.

I know a lot of people who always use throws because it is cleaner, but there is almost not much control.

+7
Jul 08 '10 at 12:15
source share

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

How to handle an exception is a broad and far from trivial question answer. It includes, in particular, a decision on where to handle the exception and what actions need to be implemented in the catch block. In fact, how to handle the exception should be a global design decision.

Therefore, when answering your questions, there is no rule of thumb.

You need to decide where you want to handle your exception, and this solution is usually very specific to your domain and application requirements.

+3
Jun 13 '13 at 13:24
source share

If the method in which the exception occurred contains enough information to process it, then it should catch, generate useful information about what happened and what data was processed.

+2
Jul 08 2018-10-12T00:
source share

A method should throw only a throws exception if it can create reasonable guarantees surrounding the state of the object, any parameters passed to the method, and any other objects that the method acts on. For example, a method that is supposed to extract from the collection an element that is supposed to be contained in it may throws exception if the element that was supposed to exist in the collection does not work. The caller who catches this exception should expect that the collection does not contain the item.

Note that while Java allows checked exceptions to create bubbles through a method declared to exclude exceptions from the corresponding types, this use should usually be considered an anti-pattern. Imagine, for example, that some LookAtSky() method is declared as calling a FullMoonException , and is expected to throw it when the moon is full; Suppose further that LookAtSky() throws ExamineJupiter() , which is also declared as throws FullMoonException . If a FullMoonException was ExamineJupiter() , and if LookAtSky() did not catch it and did not FullMoonException it or FullMoonException it into any other type of exception, then the code that called LookAtSky would suggest an exception, the result was that the Earth moon is full; he would not know that one of the moons of Jupiter could be the culprit.

Exceptions that the caller can expect to handle (including essentially all checked exceptions) should be allowed to leak through the method if that exception means the same for the calling method as it matters to the called method. If the code calls a method that is declared as throwing some checked exception, but the caller does not expect it to ever throw that exception (for example, because it considers it to be arguments that have been previously checked), the checked exception must be caught and terminated in some uncontrolled type of exceptions. If the caller does not expect an exception to be thrown, the caller cannot expect it to have any particular meaning.

+1
Jun 13 '13 at 19:16
source share

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

“But as a developer, Checked exceptions should be included in the throws clause. This is necessary so that the compiler knows which exceptions should be checked. By convention, unchecked exceptions should not be included in the throws clause.
Enabling them is considered bad programming practice. The compiler treats them as comments and does not check them. "

Source: SCJP 6 book by Katie Sierra

+1
Dec 23 '18 at 11:17
source share

If you use try catch when an exception occurs, the rest of the codes will be executed.

If you specify a method to throw an exception, then when the exception occurs, the code will stop executing immediately.

0
Mar 13 '17 at 7:26
source share

a try-catch pair is used when you want to provide customization of the behavior in case of an exception ..... in other words ... you have a solution to your problem (an exception occurs) in accordance with the requirements of your program ....

But throws is used when you don’t have a specific decision regarding the case of an exception ... you just don’t want to get your program to crash ....

Hope this is correct :-)

0
Jun 21 '18 at 17:38
source share



All Articles