This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on try catch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on try catch" Watch "Question on try catch" New topic
Author

Question on try catch

Rahul Bhosale
Ranch Hand

Joined: Mar 10, 2005
Posts: 77
I was reading the book A programmer's guide to Java Certification second edition (K A Mugal & R W Rasmussen). In the chapter on exception handling (p. 188)
The statement
For each try block there can be zero or more catch blocks, but only one finally block.
seem to be mis-leading to me which lead me to try this piece of code which obviously failed.



But it looks like if you have no catch block, finally is mandatory.
Right?


RB
Rahul Bhosale
Ranch Hand

Joined: Mar 10, 2005
Posts: 77
But later down after a couple of lines, it is said that a try block must be followed by either at least one catch or one finally block which explains it.. sorry for the trouble, i got bogged by the earlier statement.
Kristof Janssens
Greenhorn

Joined: Jan 05, 2005
Posts: 23
I want to point out something else:

there is a difference between checked and unchecked exceptions. Checked are the one the compiler found and unchecked are the one that are found at runtime(like arithmetic exceptions or array-out-of-bounds)

The remark is that if you have a checked exception (a method that throws one), you NEED a catch that can catch that specific exception, else you get compilations errors.

If you have a unchecked exception (eg. int x = 1/0 ) , catch isn't mandatory but then you need the finally. In this last case, the program will also stop running when an unchecked exception occurs.
[ March 11, 2005: Message edited by: Kristof Janssens ]

SCJP 1.4 (90%)<br />SCJD (in progress)
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi
to my knowledge
"There should be atleast one catch block for a try, there can be more than one catch block for a single try. the finally is optional one. But,the final will be executed before gettting out of the try catch block"

P.S:If any one find some thing wrong in above statement please let me know about it.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
No Parameswaran you are wrong. A try block can have 0 or more catch blocks.
Write a code and check it out.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Kristof Janssens
Greenhorn

Joined: Jan 05, 2005
Posts: 23


This will give a compile error unless you uncomment the catch block.

unchecked:


no compile error but a runtime error
[ March 11, 2005: Message edited by: Kristof Janssens ]
John Campbell
Ranch Hand

Joined: Mar 11, 2005
Posts: 53
Rahul,

The quote is right is pointing out that for each 'try' block there can be zero or more 'catch' block, but remember it is only possible to have a try block without a catch block when there is a finally block available to handle the possible exception thrown by the try block.

Again, unlike a catch block you can't have more than one finally block.

I hope this helps.
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
ok thanks
Balazs Borbely
Ranch Hand

Joined: Oct 11, 2004
Posts: 33
Originally posted by Kristof Janssens:

The remark is that if you have a checked exception (a method that throws one), you NEED a catch that can catch that specific exception, else you get compilations errors.



It is not necessary to catch all checked exceptions if the exception is specified as thrown by the method.

Example: the Kristof Janssens' example a little bit modified


'Make everything as simple as possible, but not simpler.' --Albert Einstein
Kristof Janssens
Greenhorn

Joined: Jan 05, 2005
Posts: 23
Originally posted by Balazs Borbely:
...


But you still have to catch it in the class that uses that method.
With your example you could even leave out the try and finally block. But you're right, didn't think about that
[ March 11, 2005: Message edited by: Kristof Janssens ]
Rahul Bhosale
Ranch Hand

Joined: Mar 10, 2005
Posts: 77
very very useful explanations and thanks for the providing ex snippets
 
GeeCON Prague 2014
 
subject: Question on try catch