File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes try..catch flow??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "try..catch flow???" Watch "try..catch flow???" New topic

try..catch flow???

Sagar Sharma
Ranch Hand

Joined: Aug 31, 2000
Posts: 92
when i try to compile the following code i recieve errors in the lines marked with **. The error says that "Statement not reached".
Is it true that i cannot have any line of code in the "if" blocks after the respective exceptions are thrown. i.e. is it true that "throw" should be the last statement of the block.
Please clear this doubt....

class temp48{
public static void main(String args[]){

String a=null;
String b=null;
if (a==null){
throw new MalformedURLException();
** System.out.println("bypass exception 1");
if (b==null){
throw new IOException();
** System.out.println("bypass exception 2");
catch(MalformedURLException me){
System.out.println("exception 1 caught");
catch(IOException ie){
System.out.println("exception 2 caught");
System.out.println("in finally()");
System.out.println("out of try block");
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Haven't run across this problem before, but it would seem correct that you can't have anything after the throw statement. Once you throw that exception, it is going to jump to the catch block if you have one, so anything after the throw statement would never get executed. The compiler is telling you that because it thinks you made an error, and this code should be outside of the block. This is just my thought, but it seems like that is what the compiler is doing.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3635
You are absolutely correct. Once the exception is thrown the control is out of the try block so the remaining statements ( of that block) are never reached. And that's what the compiler cribs about!

Get Certified, Guaranteed!
(Now Revised for the new Pattern)

Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
I agree. Here's the link:
subject: try..catch flow???
It's not a secret anymore!