This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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

try block

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider the following statement
"A try block must have atleast one corresponding catch block"

True or False?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ram Gopal:
consider the following statement
"A try block must have atleast one corresponding catch block"

True or False?



FALSE!

try may have only finally only.
It is not required to have both but if there is any catch block, finally
must come after that.
 
Ram Gopal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey the kathy sierra book says that
"It is legal to omit either the catch block or the finally block, but not both"

so infering the above statement, i think the foll ways are legal

1) try{}
catch{}

OR

2) try{}
finally{}

OR

3) try{}
catch{}
finally{}

but it would be illegal to use only try{} block without catch or finally, as it would give compilation error.

btw it is also mentioned that the catch block should follow try block and finally (if any) should follow catch bloak.
But what if there are 2 catch blocks and 2 finally blocks?

should the finally block immediately follow its corresponding catch block or should we write 2 catch blocks and then followed by 2 finally blocks?
like

try{}
catch1{}
finally1{}
catch2{}
finally2{}

OR
try{}
catch1{}
catch2{}
finally1{}
finally2{}

which of the above is correct one?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

which of the above is correct one?



Both are wrong. It should be like this:


 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ram

We can have number of catch blocks, but only one finally block...
So its like

try{
...
}
catch1{....}
catch2{....}
....
catchn{....}

finally{
.....
}

Thanks
 
Ram Gopal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohh ok
reply
    Bookmark Topic Watch Topic
  • New Topic