This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes TRY CATCH FINALLY BLOCKS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "TRY CATCH FINALLY BLOCKS" Watch "TRY CATCH FINALLY BLOCKS" New topic
Author

TRY CATCH FINALLY BLOCKS

khurram babar
Greenhorn

Joined: May 13, 2003
Posts: 4
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

Joined: Jan 30, 2002
Posts: 3451
For any try block, there can only be one finally block.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
yeah..
how many time one would say "final" ?
regards
maulin
louis lagos
Greenhorn

Joined: Mar 03, 2003
Posts: 6
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

Joined: Jan 04, 2003
Posts: 867
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 ]

Francis Siu
SCJP, MCDBA
Dhamodharan Krishnan
Greenhorn

Joined: Nov 07, 2001
Posts: 23
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.


Dhamodharan Krishnan,<br />Sun Microsystems, Bangalore<br />Direct : 91-80-4184 7247<br />Cell : 91-99 80 147 600
Donald R. Cossitt
buckaroo
Ranch Hand

Joined: Jan 31, 2003
Posts: 401
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 ]

doco
John Hembree
hired gun
Ranch Hand

Joined: Mar 07, 2003
Posts: 250

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

Joined: Jan 31, 2003
Posts: 401
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

Joined: Mar 11, 2003
Posts: 26
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TRY CATCH FINALLY BLOCKS