• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confusion in return and labelled break in finally

 
narendra bhattacharya
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

arithmetic exception..
this gives the output

WHY EVEN AFTER THE CONTROL TRANSFER STATEMENT IT PRINTS THE EXCEPTION BUT IN return it does not.....
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please keepItDown and some indentation would be greatly appreciated. The exception will always be printed because it's never caught.
 
narendra bhattacharya
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21007
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
narendra bhattacharya
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry wong sir i did n't understand the above khalid mughal will you please make it clear...
 
Henry Wong
author
Marshal
Pie
Posts: 21007
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21007
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
narendra bhattacharya wrote:but sir finally cannot be placed inside a block i think..


Why not?

Henry
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
narendra bhattacharya
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Henry Wong
author
Marshal
Pie
Posts: 21007
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic