| Author |
catch block.....
|
Ganeshkumar cheekati
Ranch Hand
Joined: Oct 13, 2008
Posts: 362
|
|
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?
|
SCJP5 and SCWCD1.5
Think Twice Act Wise...
|
 |
James Tharakan
Ranch Hand
Joined: Aug 29, 2008
Posts: 580
|
|
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 ]
|
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
|
 |
Himanshu Gupta
Ranch Hand
Joined: Aug 18, 2008
Posts: 598
|
|
|
Exception thrown is handled only once, not multiple times.
|
My Blog SCJP 5 SCWCD 5
|
 |
Rekha Srinath
Ranch Hand
Joined: Sep 13, 2008
Posts: 178
|
|
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
Joined: Mar 24, 2008
Posts: 3670
|
|
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.
|
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
|
 |
Pandey Gautam
Greenhorn
Joined: Nov 05, 2008
Posts: 14
|
|
Hi Ganesh, It is caught and handled in method() so why it will be caught in main() again.
|
SCJP6
|
 |
Ganeshkumar cheekati
Ranch Hand
Joined: Oct 13, 2008
Posts: 362
|
|
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
Joined: Aug 18, 2008
Posts: 598
|
|
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
Joined: Aug 29, 2008
Posts: 580
|
|
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
Joined: Oct 13, 2008
Posts: 362
|
|
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?
|
 |
 |
|
|
subject: catch block.....
|
|
|