Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Where is the right place to catch an exception?

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i know i throw an exception when a method is not able to handle it,
but what should i consider when i decide where to catch it?
its a very general question i know, but i have only just began reading about it.
thanks!
 
Marshal
Posts: 65021
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through your methods and think which method does what and where it is best caught. Where is your principal method call? Try there first.

sorry one cannot be more definite: but there is one general rule. Not in the method where it is thrown.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another general rule might be "at the point that has enough context about what was being done to be able to react to it". Low level method calls are unlikely to know what's going on, and what should be done next. At a higher level call it might know whether it should try again, log an error and carry on, show a message to the user, redirect the user to an error page, etc.
 
Campbell Ritchie
Marshal
Posts: 65021
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I meant the same as Matthew Brown, but you really put it a lot better than I did.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!