• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

try...catch block

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Examine the try…catch block that contains three statements as below:
try{
statement 1;
statement 2;
statement 3;
catch(Exception e){}
The three statements are likely to throw exception.
Question:
Is it best to use three separate try…catch blocks as below for trapping each statement:
try{
statement 1;
catch(Exception e){}
try{
statement 2;
catch(Exception e){}
try{
statement 3;
catch(Exception e){}
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim
I think you might want to have separate try .... catch blocks if
  • you are going to do something in the catch block that allows you to progress to the next step,
  • or you want to display an error message that is specific to the exception thrown and you dont believe you can easily do it in one master catch block
  • or if the try ... catch block can be thought of as logically separate from the next try catch block


  • Personally I often have large try ... catch blocks, especially with something like starting RMI and registring the service. I do several steps in the try block, then have multiple catch blocks for each type of exception.
    Regards, Andrew
    [ August 14, 2003: Message edited by: Andrew Monkhouse ]
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic