1

When is it good to put a method in a try/catch block other than the ones which are critical?

For example if we have the Pattern.compile() method and we know it can throw the exception IllegalArgumentException and the PatternSyntaxException. Should we put this method in a try/catch block? I know that a try/catch should be used when we know the method will fail. However, in this scenario a try/catch block should be used, right? How do we decide whether to use a try/catch or not because each method will throw an exception if it fails? At times the compiler will prompt us to use a try/catch for certain methods but for certain ones we have to decide ourselves. What are the factors which help us decide this ?

syb0rg
  • 8,057
  • 9
  • 41
  • 81
MindBrain
  • 7,398
  • 11
  • 54
  • 74
  • 1
    read up on java checked vs unchecked exceptions (see http://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation). Always catch checked exceptions (the compiler forces you to); always catch unchecked exceptions that you think have any probability of actually happening. In both cases, log them with a meaningful message. – tucuxi Jan 13 '13 at 23:43
  • 2
    I've voted to close this question (argumentative) but in general, once you have tested the pattern, there is little reason to use a `try/catch` around the statement, as the `compile()` methods throw `RuntimeException`s. If a pattern compiles, it compiles, no additional exception handling required. – Maarten Bodewes Jan 13 '13 at 23:44
  • 1
    @tucuxi loggig is good (we all agree that silently ignoring exceptions is generally a bad idea) but after logging you need to know how to treat them. – SJuan76 Jan 13 '13 at 23:48
  • @SJuan76 - yes, of course. But too many times I have seen that not only people do not treat them, they actively suppress them and then ignore the error messages :-P. Therefore, trying to make the world a better place, a log message at a time. – tucuxi Jan 14 '13 at 12:04

2 Answers2

3

You put the try catch where you know what to do with the exceptions.

For example, you are reading data from console and performing some arithmetic operations in the same method with the I/O. In that case, your method should control the exception, inform the user of the error, and ask for more data (or the same data corrected).

In another situation, you program a method that performs the same operation. At one time one of your expression is divided by other. What to do in a divide by 0 case? Most likely, let the exception propagate back. The calling method (at some level) will know if a failure means program crash, that an operation cannot be performed but other may, or that the data has to be ignored.

SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • Oh, and for the latter example, that does forbid catching the exception (or even prechecking the condition) and launching a new exception explaining that "using values such that the expression ... is 0 is not allowed", to help the programmers that use your method. – SJuan76 Jan 13 '13 at 23:59
  • In a few cases, it can be useful to catch an exception to add information. For example, suppose the exception is an I/O exception related to a temporary file. At some point in the call stack, you know the purpose of the failed file. Catch the exception there to throw a new one, with the old one as cause, that has a more informative message. – Patricia Shanahan Jan 14 '13 at 00:00
2

Let me explain it simply.

You don't need to use try and catch all the time. Use try and catch when you have any process that can potentially terminate your program due to an error. For example file read and write. Other than this, you can just use ifs

Jason
  • 1,298
  • 1
  • 16
  • 27