2 Replies - 202 Views - Last Post: 05 May 2019 - 06:55 AM Rate Topic: -----

#1 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2391
  • View blog
  • Posts: 5,023
  • Joined: 11-December 07

Catch-all exception handler.

Posted 05 May 2019 - 05:16 AM

This has come up twice at work recently so I wanted to ask some opinions about catching Exception:

try {
 // some code here
} catch (Exception e) {
  // do something about the problem.
}


Generally, the best practice is to catch the most specific Exception you can, but these are the two cases I was working with:

  • A "global" exception handler around the entire code for the operation, to give me a chance to log the exception details if somehting went wrong.
  • Inside a loop that sends lots of messages. If one message fails for any reason (including a RuntimeException) then I want to log it and attempt to send the subsequent messages regardless.


I chose code similar to my sample above to achieve these things. Do you think there is something better I could have done?

Is This A Good Question/Topic? 0
  • +

Replies To: Catch-all exception handler.

#2 veretimothy   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 01-April 19

Re: Catch-all exception handler.

Posted 05 May 2019 - 06:25 AM

A "global" exception handler good to log but need outside a loop
Was This Post Helpful? 1
  • +
  • -

#3 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2391
  • View blog
  • Posts: 5,023
  • Joined: 11-December 07

Re: Catch-all exception handler.

Posted 05 May 2019 - 06:55 AM

Thanks for the feedback. What about the other case, where I have it inside the loop so that one message failing doesn't prevent subsequent messages being sent?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1