• 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

dan exam doubt 14

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hii
in one question

.............................

Question 10

What is the result of attempting to compile and run the program?

a. Prints: 1,1,1,0,0,1
b. Prints: 0,1,1,0,0,1
c. Prints: 0,1,0,0,0,0
d. Prints: 0,1,0,0,0,1
e. Prints: 0,0,1,0,0,1
f. Compile-time error
g. Run-time error
h. None of the above


its answer is : f with explanation

A throw statement is the first statement in the outer try block. A throw statement appearing in a try block causes control to pass out of the block. Consequently, statements can not be reached if they appear in the block after the throw statement. The switch statement that appears after the throw statement is unreachable and results in a compile-time error.

I am not able to understand ...the why this option is choosen as well as its explanation

[ Code tags added and, I hope, formatting improved ]
[ February 24, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit,
Explanation is enough to understand the problem in the code. What part you didn't understand ?
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i m not able to understand that why "c" is not the option as after "throw" in out try ...the flow of programe should directly transfer to Level1Exception e ..???


and i don't get any point in explanation what Dan is expaining in his answer ....sorry..
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry in above i wrote why "c" is not the option ..i mean why "h" is not the option ?
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit,
Lets simplify the problem. Compile the following code and see if you can understand what's happening.

Here is the link in JLS about unreachable statements.
click here

Hope this helps you
[ February 24, 2005: Message edited by: Jay Pawar ]
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can understand that you may have questions about the explanation, but the answer of (f) seems pretty straightforward if you try to compile and run it like the question suggests.

 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx for reply...but can anybody please explain the explanation given by dan ...in more details...

see we are just sharing our thougths....so..it will be good for us to explore our world...

Thanx and Regards,
amit
 
Carol Enderlin
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which part are you not getting? Sure, someone could explain this line by line, but wouldn't you get more out of it by working through it?

Dan exam explanation about line 12 being unreachable:
1. A throw statement is the first statement in the outer try block.

2. A throw statement appearing in a try block causes control to pass out of the block.

3. Consequently, statements can not be reached if they appear in the block after the throw statement.

4. The switch statement that appears after the throw statement is unreachable and results in a compile-time error.

-- you can wait for someone else to explain it, but you said you want high score so I'm trying to push you.
[ February 25, 2005: Message edited by: Carol Enderlin ]
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok got it... it means the Exception1 e is catching up...and thats why compile time error is comming...

guys you know that was my earlier ..but i misundestood and make and query..
i forgot that ..catching an exception will give compile time error ?


is i m right guys.?
 
Carol Enderlin
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you're saying in the last post.

The compile time error is about an unreachable statement.

Because an exception is thrown you can never get to the try block with the switch statement in it.
[ February 27, 2005: Message edited by: Carol Enderlin ]
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think that doesn't matter....that ..inner try block is never executed !!
i think what matter is as soon as exception is thrown..wheather explicitly(as in this case ) or implicitly ( due to our programming error) ...
complier will show complier error

please correct me ...if i am wrong..

amit
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit , when you are throwing exception intentionally , it means you don't want to compiler to go deep inside ...
So other statements are unreachable ....
hope it may help .

but I have one doubt from JLS :






why first one gives compile time error & second one compiles fine ...


one more thing is , JLS is deep enough for just a small topic ( unreachable ) K&B book gives not enough information about like that ...
is it risk to follow K&B ??
 
Carol Enderlin
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's OK to throw an exception. This code compiles without error:



The compiler will complain if there is a statement that will never be reached:

 
Carol Enderlin
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rathi,

I guess you just needed to read a little more on unreachable statements in JLS (14.20 Unreachable Statements ), it says it's to allow conditional compilation.


The rationale for this differing treatment is to allow programmers to define "flag variables" such as:


static final boolean DEBUG = false;

and then write code such as:

if (DEBUG) { x=3; }

The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.

 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carol , There is lot in JLS ..
But I am sure that there would be some logic behind this , that I am not able to get , please if you or any body got the idea then please help ...

Thanks .
 
Carol Enderlin
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rathi,

I pasted the relevant part of JLS. JLS says it is to allow conditional compilation.
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx ....
i got the point..i posted originally


but i don't get any point which rathiji post ...

can any body explain in easy words and details..?
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding the doubt by Rathi Ji,

In JLS for non reachable statements in a while loop it states as:


A while statement can complete normally iff at least one of the following is true:
The while statement is reachable and the condition expression is not a constant expression with value true.
There is a reachable break statement that exits the while statement.
The contained statement is reachable iff the while statement is reachable and the condition expression is not a constant expression whose value is false.



Just read the above bold statement carefully, it says that if the condition expression is not a constant expression whose value is false, the statement is reachable

but

also



Now look for the rule for if statement


The actual rules for the if statement are as follows:

ACTUAL: An if-then statement can complete normally iff it is reachable.
The then-statement is reachable iff the if-then statement is reachable.


Look out for the bold statement above, the then statement(The statement just after if statement) is reachable if and only if if-then is reachable. Here nothing is mentioned regarding constant expressions....

Thats why


Hope this makes u clear
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Animesh ...is this true

as the condition expression is a constant expression and evaluates to false.
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest u better look into JLS for more info.
Just look up for the rules for unreachable in a for loop.
The link is already given by Carol Enderlin in a previous message
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic