Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception handling

 
pratapsiva sivakumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sir,
I am using a try{ some code} and then finall{some code} block.I am not using catch block.So if an exception is thron in try block,how to handle it.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pratapsiva sivakumar wrote:Hi sir,
I am using a try{ some code} and then finall{some code} block.I am not using catch block.So if an exception is thron in try block,how to handle it.


I believe the way you are doing it is the same as catch( Exception e ) {...}, except you don't get an exception object to work with. So youhave to make up your own message.
 
Filipe Pinheiro
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should either declare or manipulate it inside of the caller method
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Filipe Pinheiro wrote:You should either declare or manipulate it inside of the caller method
...


Hold on. If anything ever gets thrown in that situation, that means my answer that a finally is like a catch without the exception object to work with must be wrong.

Filipe, do you see any possible value in having a try without a catch?
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any checked exception must either be caught or declared to be thrown. So if you don't catch it, throw it on:
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob for the example. I hadn't considered that there could be an exception other than that caused by a non-existent file.

I guess there might be a reason why you would want one or both exception types to be handled further up the stack, and still deal with closing the Reader within its scope. hence the try without a catch. Which means of course there is a reason for declaring the reader locally as opposed to passing the reader instead of the file name to this method.

regards.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic