File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confusion in return and labelled break in finally 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 "confusion in return and labelled break in finally" Watch "confusion in return and labelled break in finally" New topic
Author

confusion in return and labelled break in finally

narendra bhattacharya
Ranch Hand

Joined: Feb 17, 2010
Posts: 66

arithmetic exception..
this gives the output

WHY EVEN AFTER THE CONTROL TRANSFER STATEMENT IT PRINTS THE EXCEPTION BUT IN return it does not.....

SCJP1.6,SCWCD1.5
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Please keepItDown and some indentation would be greatly appreciated. The exception will always be printed because it's never caught.


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

Joined: Feb 17, 2010
Posts: 66
what are you saying that if the exception is caught it always print exception...i think .. you forgot the concept when control transfer used in finally
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

narendra bhattacharya wrote:what are you saying that if the exception is caught it always print exception...i think .. you forgot the concept when control transfer used in finally


This question is definitely out of context... so here's some context.

http://www.coderanch.com/t/488846/Programmer-Certification-SCJP/certification/Exception


In the previous example, the OP has a return from finally... which when done will hide any return or exception from the try and catch blocks. In this example, the finally clause has no such return, so it doesn't hide the runtime exception. After the finally is finished, the runtime exception continues, and the exception is printed by the JVM.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
narendra bhattacharya
Ranch Hand

Joined: Feb 17, 2010
Posts: 66
but sir in khalid mughal book..


it is given
"if finally block neither throws exception nor executes a control transfer statement like a return or a labeled break ,the execution of the try block or any catch block determines how execution proceed after the finally block "
narendra bhattacharya
Ranch Hand

Joined: Feb 17, 2010
Posts: 66
Henry wong sir i did n't understand the above khalid mughal will you please make it clear...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

narendra bhattacharya wrote:but sir in khalid mughal book..

it is given
"if finally block neither throws exception nor executes a control transfer statement like a return or a labeled break ,the execution of the try block or any catch block determines how execution proceed after the finally block "


That quote... refers to a label outside of the finally block -- meaning getting around the normal completion of the finally block. In your example, your label break merely causes a jump to within the same finally block.

Henry
narendra bhattacharya
Ranch Hand

Joined: Feb 17, 2010
Posts: 66
but sir finally cannot be placed inside a block i think..
and placing a labeled break must be inside the labeled block....
then
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

narendra bhattacharya wrote:but sir finally cannot be placed inside a block i think..


Why not?

Henry
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

narendra bhattacharya wrote:
arithmetic exception..
this gives the output

WHY EVEN AFTER THE CONTROL TRANSFER STATEMENT IT PRINTS THE EXCEPTION BUT IN return it does not.....


Your question is not clear.

If you ask about control transfer, I can give a example.



The return value from this block or method is 300 (ie: i = 300). Hope, itt'll help you!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
narendra bhattacharya
Ranch Hand

Joined: Feb 17, 2010
Posts: 66
Henry Wong wrote:
narendra bhattacharya wrote:but sir finally cannot be placed inside a block i think..


Why not?

Henry



sir finally can be placed only when try as well as catch are placed in the SAME BLOCK..

I UNDERSTOOD SIR


this will give output .. here
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
I'm going to go out on a limb here...

For sure, you won't find any such question on the real exam, breaks won't be mixed together with exceptions.

Here's where I'm going out on a limb... in the old days the exam creators forced you to memorize detailed precedence rules, and other such trivia. For the last several years the exam creators have taken, what I think is a better approach. The new approach is that, whenever the prometric tools allow it, emphasize good coding practices. So, for instance, the exam now favors using "extra" parentheses to make the code clear, even if, officially, the compiler will do the *right* thing.

With all that said, I would say that putting breaks into exception handling is just a bad practice. There I said it. Am I about to get lambasted?


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Bert Bates wrote:With all that said, I would say that putting breaks into exception handling is just a bad practice. There I said it. Am I about to get lambasted?



I'll add to that... I say that hiding returns (or exceptions) by changing the result (or just not completing) in the finally cause is not good practice. I also say that using label breaks is considered by many to be not good practice.

The example in this topic does a lot of "bad practices".

Henry

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: confusion in return and labelled break in finally