-2

I have the following method in Java:

public method1() {
  //does something that might throw an exception
}

And then another method that calls the method above like so:

public method2() {
    method1();
}

What is the convention here in regards to try-catch : Should it be in method1() or in method2()?

3
  • 3
    It depends on which method cares about the exception. If you don't know that, maybe there shouldn't be two methods.
    – Ant P
    Commented May 22, 2019 at 9:53
  • 1
    Catch exceptions when you know what to do to handle them, like retry or inform the user. Could be that this is somewhere in method2's caller or caller's caller, i.e. further up the call chain. If you cannot find a place or don't know what to do, then don't catch them, and let the program terminate, until you can identify a more desirable behavior.
    – Erik Eidt
    Commented May 22, 2019 at 14:25
  • I think it is helpful to think of uncaught exceptions as multiple return types: after all, it is an "outcome" of executing the method. So, if it makes sense for a method to return a value type or an error type, don't handle the exception; if the method should always return a value type, handle the exception in the method. Commented May 23, 2019 at 7:52

3 Answers 3

7

With no other context: Neither.

The correct place to put the catch is "at the point where something useful can be done about the failure".

This is not the same as needing to add try-finally or try-with-resource around potentially throwing methods, which obviously can only go where the things they refer to are.

3
  • There seems to be consensus to only catch exceptions if you know how to handle them, but how would you deal with cron/batch processing tasks where you don’t want the process to end if a method inside a loop can throw exceptions?
    – Rik D
    Commented May 22, 2019 at 16:30
  • 1
    @Rik Catch the exception, log it [that counts as exception handling], carry on if you can and want. Commented May 22, 2019 at 17:36
  • @RikD "The main loop" is an instance of "where something useful can be done", viz "continuing with other processing"
    – Caleth
    Commented May 22, 2019 at 19:15
1

I assume you mean Method2 calls Method1 (as currently written Method2 calls itself recursively).

You can put the catch-block in either place, depending on what you intend to do about the exception (and there are many possible ways in which an exception can be responded to). There is no general answer in the abstract.

1

Don't catch an exception if you can't (or until you can) do something useful about it.

There will be exceptions which you can't do anything about. There will be exceptions which you haven't thought about. Those kinds of exceptions are often critical or fatal. Exceptions like that are rare in a well-developed application. But, you certainly want to know if they occur. For those kinds exceptions, have a global(ish) handler(s) which cover the entire application and log the exceptions, come what may. The location of these umbrella exceptions handlers depends on the nature of the application, architecture, framework.

Not the answer you're looking for? Browse other questions tagged or ask your own question.