File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes when will the exception will be caught ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "when will the exception will be caught ???" Watch "when will the exception will be caught ???" New topic
Author

when will the exception will be caught ???

srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
hi frnds ,

jus look at the code....

import java.io.*;
class Test {
public int myMethod1() throws EOFException {
return myMethod2();
}
public int myMethod2() throws EOFException {
// Some code that actually throws the exception goes here
return 1;
}
}

here myMethod2 throws the End of file exception which is a checked exception ,also myMethod1 which called the method2 throws the exception ...
so my doubt is when will the exception b caught ...(i.e where will b the catch block b)

and also how to handle the runtime exception......

thanks & regards

srikanth reddy


Thanks & Regards<br /> <br />-Srikanth
Sergei Iakhnin
Ranch Hand

Joined: Nov 09, 2004
Posts: 53
The catch block will be wherever you put it. Otherwise the program will crash upon throwing the exception.


SCJP 1.4, SCWCD 1.4
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
sorry i didnt get u ....

i have two doubts regarding the above code ..

1)i have tried compiling the above code it says statement not reachable
i.e after throwing an exception it doesnt return 1...so can i say that the
complier dont continue with the rest of the code(after the exception) like
the "continue".

2)in the code no method is catching the exception (all throws the exception)even the calling method also .is it right ???

please can u clarify quickly so that i can move fwd with my preparation .

thanks & regards

srikanth
Sergei Iakhnin
Ranch Hand

Joined: Nov 09, 2004
Posts: 53
Since no code explicitly catches the exception the default exception handling mechanism will be employed which will result in program termination and printout of the stack trace.

When a checked exception occurs it needs to be handled in order for normal execution to resume. It is not right to only throw the exception and never catch it unless you are satisfied with the default exception handling behaviour.
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580


1)i have tried compiling the above code it says statement not reachable
i.e after throwing an exception it doesnt return 1...so can i say that the
complier dont continue with the rest of the code(after the exception) like
the "continue".


Once a excception is ocurred it should be catched and if it satisfies the condition for continuing, then execution flow will continue

In the above code if use try & catch block use return statement in both the blocks otherwise have it in finally block

Then code will compile


in the code no method is catching the exception (all throws the exception)even the calling method also .is it right ???


yes, we can have like that then compiler handles it.(ensure no return statements in it] if so we should use try along with catch or finally blocks

hope it clears
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
Any method that calls checked-exception-throwing methods must deal with the exception in one of two ways:

1) The method declares that it throws the exception type. That's what your myMethod1() does: it calls myMethod2(), which throws EOFException. Since myMethod1() also throws EOFException, there's no problem. Until you actually want to call myMethod1() somewhere.

2) The call to the exception-throwing method must appear in a "try" block, with appropriate other stuff. For example:



Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: when will the exception will be caught ???
 
Similar Threads
Assert statements
class methods vs instance method
synchronized method
Exception Handling
Exception Handling