Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Doubt

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • 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?
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the code with try blocks added?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With ^^, because this compiles just fine

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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 ....

 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Look for these types of questions also.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks a lot Guys!

Punit, the example was very Good.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic