File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes About UnReachableCode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About UnReachableCode" Watch "About UnReachableCode" New topic

About UnReachableCode

kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
Hi there,
Well can you people tell me when exactly "UnReachableCode" error occurs?
I was tried following examples--
public class ExitCatch
void meth()
System.out.print("inside try");
}catch(Exception e){
System.out.print("inside catch");

public static void main(String[] args)
ExitCatch ob = new ExitCatch();

Here i thought that UnReachableCode error will occur because any how System.exit(0) will terminate the program, so control will never reach to catch. But I didn't get that.

Then i tried this example--
class My
void meth()
System.out.print("Inside the catch");
}catch(Exception e){}
catch(FileNotFoundException e){}
Thank God! Here i got what i thought.
But the problem is this that i am unable discriminate these two.
please help me out.
Thank you in advance.

Steve Luke

Joined: Jan 28, 2003
Posts: 4167

In the first example, you are catching the generic Exception, which is the parent type for all checked exceptions but also the parent of all unchecked exceptions. That would mean that it may be possible to reach the catch clause before the System.exit was called (as far as the compiler could predict).

The second example you use a checked exception - FileNotFoundException. FileNotFoundException will only occur in methods whose signatures declare that they throw the FileNotFoundException (which is the nature of checked exceptions). Since none of the methods you call could possibly throw a FileNotFoundException the compiler knows the catch can not be reached.

kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
Hi Steve,
Thanks for your reply. Well I am not bothering about catching the exception. I want to know when the "UnReachableCode" error will occur. What i thought in first example that due to use of System.exit(0) will break the program execution, always(am i right?), and control will never reach to associated catch block. This is my problem.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41108
Well I am not bothering about catching the exception.

But you should, because that is -as Steve explained- the answer to your question.

Why do you think the catch block can't be reached in the first example? What if a RuntimeException (like a SecurityException) occurs?

Ping & DNS - my free Android networking tools app
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Also, System.exit is just an ordinary method call. The compiler doesn't know that it ends the program.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Anand Hariharan

Joined: Aug 22, 2006
Posts: 257

Most unreachable code is a compiler error in Java. So, if you have unreachable code, you will be told about it by the compiler.

Look up JLS for the gory details:

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
Thank you Dittmer, Preuss, Hariharan
I agree. Here's the link:
subject: About UnReachableCode
Similar Threads
Exception treated differently
Finally method
Queston on exceptions
"Double" and "double" - what's the difference?