• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

try..catch block

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A method can have mutiple try/catch block, but I always
use one try/catch block ( may be i have seen more code
of one try/catch block). Below i have two sample methods.

Which one is appropriate ?
Regards,
Akash
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aakash,

Which one is appropriate ?


Both approches are fine. It all depends on how you want to handle the exceptions for methods.
You may use Case 1 type when you want to handle exception for methods individually... On the other hand case 2 is for combined or single exception handling for both methods.
So its just the matter of how you want to handle exceptions.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The important question here is: if methodA() throws an error, do you want to perform methodB() next, or not? In many cases the answer is no, because methodB() is written in such a way that it assumes methodA() has already been completed, and if that's not the case, methodB() may well be meaningless, or throw another error. So I'd say that option 2 is usually what you want. But there are times when you really do want methodB() to be performed even if methodA() fails - in this case, option 1 is preferred. I don't think we can say any more without knowing what methodA() and methodB() are supposed to do.
 
Akash Singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Very Much, Jim and Chetan.
Regards,
Akash
 
So you made a portal in time and started grabbing people. This tiny ad thinks that's rude:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic