• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

multiple try blocks

 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You guys are so great! Many, many thanks!

    Your examples and explaination were brill.
     
    The human mind is a dangerous plaything. This tiny ad is pretty safe:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic