• Post Reply Bookmark Topic Watch Topic
  • New Topic

multiple try blocks  RSS feed

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you confirm whether or not it is bad practice to use multiple try blocks?

i.e I would like to put the following in the try and catch block



I have NOT come across the use of multiple try blocks. Is there a reason for this?

AND if multiple try blocks are bad practice? What do you do as an alternative? Do you repeat the catch block in another try block?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I've read that nesting try/catch blocks can be a good idea because it makes the code easier to read. It's clear what each block is "trying" and what it's intended to "catch."

Note, however, that each try block needs an associated catch or finally.
[ April 15, 2005: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that I look at your code again, I'm confused about why you're doing this. Character.isDigit(char c) doesn't throw anything. So are you really looking for something like this...?
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc, as always thank you for your reply. Much appreciated!

I tried the following code:



But I get the following error:

C:\java\PoliceDatabase>javac TestApp.java
.\VehicleRecord.java:426: 'try' without 'catch' or 'finally'
try {
^
1 error

Surely the catch block catches both try blocks?
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Edit: made the last code example use nested trys)

A couple things.
  • Character.isDigit() doesn't throw an Exception if the charactr isn't a digit. It just returns false.
  • A try/catch block will catch Exceptions from any statment that throws an Exception.


  • So your code should be modified to...

    However, let's say that you use methods that might throw an exception:


    The "catch" will catch any exception thrown by either of the parseInt() calls.

    When would you nest try/catch blocks? I could see it when trying to do file or network I/O.

    Pseudocode:


    But notice that each try has its own catch. One catch cannot "service" more than one try.

    HOWEVER...
    One try may have more than one catch:


    There, that ought to be more than you wanted to know.

    Ryan
    [ April 15, 2005: Message edited by: Ryan McGuire ]
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Each try requires its own catch or finally.

    You could do something like this...

    Note that you can put as many statements as you need within the try block. You don't need a separate "try" for each one.
     
    Maureen Charlton
    Ranch Hand
    Posts: 218
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You guys are so great! Many, many thanks!

    Your examples and explaination were brill.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!