aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception Doubt" Watch "Exception Doubt" New topic
Author

Exception Doubt

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
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

Joined: Apr 16, 2004
Posts: 201
Can you post the code with try blocks added?


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

With ^^, because this compiles just fine



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

@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 ....



SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
:| Are you sure about this? I checked the both programs, neither I got a compile error!
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509


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

Joined: Nov 12, 2008
Posts: 757
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

Joined: Oct 25, 2008
Posts: 2700

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

Joined: Sep 16, 2008
Posts: 509


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

Joined: Oct 16, 2008
Posts: 952


Look for these types of questions also.


SCJP 6
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
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

Joined: Sep 16, 2008
Posts: 509
Thanks a lot Guys!

Punit, the example was very Good.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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.


All code in my posts, unless a source is explicitly mentioned, is my own.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Doubt