Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions

 
Sanjeev Gupta
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following code :
import java.io.*;
import java.net.*;

public class Base{

private void test() {

try {
String a = null;
String b = "b";
// Complex processing

if(a==null)
throw new MalformedURLException("test");
if(b ==null)
throw new EOFException("test");
// Complex processing

System.out.println("End of try block");

}
catch (MalformedURLException e) {
System.out.println("Caught MalformedURLException");
return;
}
catch (EOFException e) {
System.out.println("Caught EOFException");
return;
}
finally {
System.out.println("End finally");
}
System.out.println("End processing");
}

static public void main(String[] a) {
new Base().test();
}

}
Could somebody explain why "End Processing" is not printed out?
Thanks.
 
Seema Das
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you have return statement in your catch block, it will print "Caught MalformedURLException", then it will execute finally block and then return. It won't execute any code after finally.
-Seema
 
Sanjeev Gupta
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Seema, I somehow overlooked the 'return'. The eyes have to be really open!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic