Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

catch block.....

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SOURCE:www.javabeat.net



Output is:
Try block1
Catch1:java.io.IOException: exception1
Finally block1:
But here method is throwing an exception which is caught by catch block in main method.
Why it is not printing �catch2:� in output?

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception is caused during int u=method();

When assigning the value it calls the method. And in the method Exceptionm is caused .which leads to its cath and finally{} and execution ends...
it won't even execute the return statement.
[ December 03, 2008: Message edited by: James Tharakan ]
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception thrown is handled only once, not multiple times.
 
Rekha Srinath
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its because you have already caught your exception in the catch block of method().. Exceptions once caught are not caught again.

If you did not have a catch() block in method() AND if you had only declared that exception in method(), then the "catch2" would have been executed.
 
Vijitha Kumara
Bartender
Posts: 3908
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why it is not printing �catch2:� in output?


Because you have already catched the exception in catch block of the method() . So,rest of the code in the main will continue after
.

Normally you wouldn't do this in real development. You are announcing that the method throws the IOException but you have catched it inside the method itself.
 
Pandey Gautam
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ganesh,
It is caught and handled in method() so why it will be caught in main() again.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i am not catching exception in main mathod it is giving me compilation error as

IOException must catch or throw...

so i should catch..

do you mean that it is just to specify not to catch because it is already caught in method?
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the signature of your method

static int method() throws IOException

which says that the method which will call it has to handle the exception if thrown.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the method your are throwing an exception and you are also catching the Exception.
So the exception is handled.
But in the signature of the method ,
static int method() throws IOException
your are saying that the method would throw an exception which it will not handle. In such a case the main method shouuld handle it.

you can very well do as follows




 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes main method should handle that IOException so i am catching it.

but already IOException is handled in method so it is not handled in main method because exception should handled only onetime...thats why it is not displaying "catch2" in output...

am i right?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic