• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

About UnReachableCode

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.










}
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, System.exit is just an ordinary method call. The compiler doesn't know that it ends the program.
 
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Dittmer, Preuss, Hariharan
reply
    Bookmark Topic Watch Topic
  • New Topic