Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TRY CATCH FINALLY BLOCKS

 
khurram babar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try block is single , catch blocks r multiple for try block ok
can u tell me is finally block is multiple or single ?
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For any try block, there can only be one finally block.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah..
how many time one would say "final" ?
regards
maulin
 
louis lagos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try block is a way to perform your program .
catch blocks tells you or handle what it would be if something goes wrong.thus you must protect your programe and inform the user with all the
necessary catch blocks.
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
khurram
can u tell me is finally block is multiple or single ?
Michael said the truth.
I think the question is not tricky but the concept you need to know
Finally block can be single or zero when the try block is single.
Because placing the finally block in the coding is not mandatory.

[ May 17, 2003: Message edited by: siu chung man ]
 
Dhamodharan Krishnan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Khurram,
Hope you know Cricket.
try
{
code and its Exection // bowler and batsman
}
catch( Exception1 ) // fielder1
{

}
catch( Exception2 ) // fielder2
{
}
...
finally
{
// Umpire
}
Bowler bowls - Programmer does the code
Batsman bats - Your computer executes
Fielders - Your catch blocks
Umpire - What he decides is finally.

If it is thrown again in any of the catch block,
Then only Third umpire should catch it
Dhamo.
 
Donald R. Cossitt
buckaroo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is a sticky wicket. What if there are multiple try calls? Should there be a catch for each try or multiple catches for each try with only one final or multiple finals? I think this is what is being asked.
Would it not be true to say only one final call regardless of the number of try / catch combinations?
EG

Somebody tell me if I screwed something up, but I think this is a general idea? :roll:
[ June 03, 2003: Message edited by: Donald R. Cossitt ]
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doco I think you example looks good, you can have nested try/catch blocks, I believe from a syntax perspective though you have to have as a minimum a try/catch(s) or a try/finally and of course you can always do try/catch(s)/finally.
Although you show you're nested try/catch calls here in one method, I think you will also find them scattered thru different method calls and different classes where the nesting may not be as apparent.
This would allow the exception to bubble up to the calling method or try/catch block for handling.
 
Donald R. Cossitt
buckaroo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah it is a bit of a cobble. I was just being a bit outrageous for effect Marilyn and Jason have been doing a great job working that out of me though.
 
Helen Crosbie
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't forget that it is important to put your catch blocks in the right order if you're trying to catch more than one exception. The most specific exception should be put first:
e.g.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic