aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes An Exception question in Dan's mock exam 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 "An Exception question in Dan Watch "An Exception question in Dan New topic
Author

An Exception question in Dan's mock exam

Yi Dong
Greenhorn

Joined: May 19, 2003
Posts: 22
The question is:
class ColorException extends Exception {}
class WhiteException extends ColorException {}
abstract class Color {
abstract void m1() throws ColorException;
}
class White extends Color {
void m1() throws WhiteException {throw new WhiteException();}
public static void main (String[] args) {
White white = new White();
int a,b,c,d;
a = b = c = 0;
try {
white.m1();
a++;
} catch (WhiteException e) {b++;}
finally {c++;}
System.out.print(a+","+b+","+c);
}
}
I thought a++ would never be reached, so there should be a compile-time error code not reachable, but it compiled fine. Can anybody explain to me why there is no such error, can a++ ever be reached ?
Thanks in advance
Yi
pradeepbill arumalla
Greenhorn

Joined: Jul 15, 2003
Posts: 19
hi there,
here is what i think,there is absolutely no problem if there is an unreachable statement in try block iff u have a proper Catch to catch the Exception thrown...please use JLS from the below link on unreachable statements...there is no special mention on try{} block unreachable statements...since the exception is thrown in between { & } it can be any where before or after a statement.
http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#236365
thanks


pradeep
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
A Java compiler must carry out a conservative flow analysis to make sure all statements are reachable. This analysis takes into account the structure of statements.

A method invocation can completely normally. Therefore, a statement following a method invocation statement is reachable.
When you and I look at the method m1, we think - well, that method will never complete normally. But it is not one of the rules for the compiler to analyze the method. (see JLS 14.20 Unreachable Statements)
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787

If you try above code, it will give compile time error you are looking for. Compiler checks upto certain level for unreachable code.
Thanks
Barkat
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: An Exception question in Dan's mock exam