File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on try catch

 
Rahul Bhosale
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rahul Bhosale
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1228
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Parameswaran you are wrong. A try block can have 0 or more catch blocks.
Write a code and check it out.
 
Kristof Janssens
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks
 
Balazs Borbely
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kristof Janssens
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very very useful explanations and thanks for the providing ex snippets
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic