• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

try...catch block

 
Tim Adam Cooper
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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){}
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • 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 ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic