• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

help in exceptions

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many catch blocks can be associated with a try block???
Does every try block has one and only one catch block?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To answer your questions: As many as you like, No.

Why don't you write a small test program to try it out? Experimenting is a great way to learn.
 
Erum Momin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, i think that's the great way...
I was trying to solve this by writing the small program...
Thank you.
 
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed, you can have many, many catches, since there are many different exceptions a piece of code might throw. You can only have one finally block.

-Cameron McKenzie
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But look out for the unreachable code!
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There can even be no catch block at all.
just -
try{}
finally{}
then the exception,if thrown, will be ducked through the call stack.




 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use that quite a lot, Priya. Often the method that causes the exception can't add anything meaningful and even logging is pointless; so I let it bubble up to the first methods that has some clue about what is going on (and it stops the log file getting cluttered with every method in the stack logging the same exception again and again and again... ...I hate that).

And roll on Java 7 where we can catch multiple Exceptions in the one block!
 
priya rishi
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello jason,
I mentioned try block without catch only for the purpose of exam.
but in real scenario, whats the use of having try block without catch
and even one catch block for multiple exceptions.

 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can have a try-block with just a finally, and no catch-block, if you don't want to catch the exception, but you do need to do some cleanup in case an exception occurs. For example:
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesper gives a great example about what I meant with no catch block. There's no point in catching in unless you can do something about it, log some pertinent information that will not be in the exception, or wish to wrap the exception as the one caught is not discrete enough.

As for multiple exceptions in one catch block, it saves having to write the same logging/handling code multiple times (or placing in a method somewhere). Sometimes the way different exceptions are handled happens to be the same.
 
priya rishi
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper and Jason, now i get the concept.
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As many catch blocks as you like, but remember the narrowest Exceptions must be caught first then the widest, for example catch an IOException before an Exception.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic