• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unreachable Code Question

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt regarding this piece of code from JQ+ mocks and hope somebody can suggest something.



When compiled, it prints B and also throws an exception.
But my question is that shouldn't the line 2 flag compile error, since it is unreachable code (Line 1 throws an exception).
This has made it a little confusing. How do you determine when the code is unreachable ?

thanks,
Roopesh.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes (okay, most times), we're just smarter than a compiler. We can see from looking at that bit of code exactly what's going to happen and we know that line 2 will never get executed. The compiler, however, is not as bright.

What does the compiler see? Well, when it looks at line 1, it checks to see if that method exists and if it can throw an exception. Sure enough, m1() exists and it has a "throws Exception" clause in the signature. Therefore, the compiler thinks, "Hmm...m1() might throw an exception so I'm going to force the programmer to put this method invocation into a try block." However, the compiler does not look into the method m1() to see if an exception will be thrown or not. As far as the compiler is concerned, m1() might throw an exception, or it might not.

Because the compiler doesn't know if m1() will throw an exception or not, it also doesn't know if line 2 will ever be executed. It's quite possible, from the compiler's point of view, that m1() will terminate normally and throw no exception, causing line 2 to execute. Therefore, no error.

I hope that helps,
Corey
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roopesh,
well i dont think that the statement:
System.out.println("A"); // Line 2
is unreachable.
the compiler just sees that you have called a method before this statement. and it does not cares if that method throws an Exception.

Had you directly replaced the line
m1(); with

throw new Exception();

it would have surely raised a compiler error: unreachable code.


Sandy
 
Roopesh Gulecha
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Corey and Sandeep. It really helped.

Roopesh.
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roopesh-

Corey is right. We're smarted than the compilers (most of the time).

I've found that while studying for certification I've overthought questions at times and gave the compiler more credit than it deserved. For instance, I thought final char myChar = "My String Literal".charAt(0); could be considered a compile time constant and used as a case in a switch because it was obvious to me that myChar could be fully resolved at compile time and was final. It's not so obvious to the compiler since it's dealing with the results of a method call.

All that to say, I've had to make a conscious effort to keep a question in a strict context (in this case: What is the job of the compiler?) and not to read too much into them.

Josh
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic