• 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

Exception Doubt

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



Here if Line 1 and Line 2 would have been in the Try block, we would have got Unreachable statement, Compiler Error, why not in this case?
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the code with try blocks added?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With ^^, because this compiles just fine

 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Abhi vijay,
I dont think such a question would turnup in the real exam even though its in Examlab.( this is my personal option ! !)
The unreachability with respect to if is a bit complex.Check out the JLS 14.21. Even they have discussed it separately.
BElow is what they say.
Hope it helps you ....

 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
:| Are you sure about this? I checked the both programs, neither I got a compile error!
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Now I get a Compiler Error, saying UNREACHABLE statement. But when it is in the if block, there is no such error.
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aha it is very simple problem. For the exam, you should keep in mind that the if expression will not evaluate by the the compiler, for the purpose of finding unreachable statements.

For an example:



This is ok, because the compiler don't evaluate the if condition, so it don't know whether the exception will throw or not.


Warning: But you should note that if expression will be evaluate, for the purpose of checking for local variable initialization. For an example:

int x;
if(true) x=1;
System.out.println(x);


The above code compiles, but the code below will not:

int x;
if(false) x=1;
System.out.println(x);



Keep in mind the above two concepts for the exam. I remembered that I got a question on the exam, that uses the if(true) to check unreachable statements.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Treimin Clark wrote:
Warning: But you should note that if expression will be evaluate, for the purpose of checking for local variable initialization. For an example:

int x;
if(true) x=1;
System.out.println(x);


The above code compiles, but the code below will not:

int x;
if(false) x=1;
System.out.println(x);



Only if the statement uses final variables otherwise compilation will fail because the variable might not have been initialized.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In this case when throw new Exception(); then an Exception is thrown and the associated catch block executes, then the control never comes back to the method, so System.out.println("e"); is an Unreachable statement??? Whys is the compiler not detecting it?
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Look for these types of questions also.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhi vijay wrote:

In this case when throw new Exception(); then an Exception is thrown and the associated catch block executes, then the control never comes back to the method, so System.out.println("e"); is an Unreachable statement??? Whys is the compiler not detecting it?



Well I raised such a question a long time ago. It is a strange kind of behavior of the compiler. If you initialize a field in a if(true){} block, then the compiler knows that the field will be initialized, but if you throw an exception in the if(true){} block, then the compiler doesn't flags the next statement of the if block as unreachable. That's the way it is. You will have to memorize it. Bert also confirmed that this type of behavior will not be tested on the exam so don't worry...
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I clearly remembered that I got a question like this on the real exam too. They used if(true) condition before throwing an exception.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Guys!

Punit, the example was very Good.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the reason the compiler doesn't take into account if statements for the purpose of unreachable statements is to support "conditional compilation." The whole purpose of conditional compilation is to make specific statements unreachable. However, if the unreachable statements involve necessary local or final variable initialization code, then the compiler will complain.
 
It will give me the powers of the gods. Not bad for a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic