• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About UnReachableCode

 
kayanaat sidiqui
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
Well can you people tell me when exactly "UnReachableCode" error occurs?
I was tried following examples--
public class ExitCatch
{
void meth()
{
try
{
System.out.print("inside try");
System.exit(0);
}catch(Exception e){
System.out.print("inside catch");
}
}

public static void main(String[] args)
{
ExitCatch ob = new ExitCatch();
ob.meth();
}

}
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()
{
try
{
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
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, System.exit is just an ordinary method call. The compiler doesn't know that it ends the program.
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.21
 
kayanaat sidiqui
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Dittmer, Preuss, Hariharan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic