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 An Exception question in Dan's mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "An Exception question in Dan Watch "An Exception question in Dan New topic

An Exception question in Dan's mock exam

Yi Dong

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 {
} catch (WhiteException e) {b++;}
finally {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
pradeepbill arumalla

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.

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.
I agree. Here's the link:
subject: An Exception question in Dan's mock exam
It's not a secret anymore!