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 question about exception 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 "question about exception" Watch "question about exception" New topic
Author

question about exception

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi guys
I am not clear about Exception part. Would you please take a look at the following code:


The output is 13423. I am not quite sure how the program goes
after it catch the exception. ( I mean why the result is not
like 134234? )
Thank you very much!


[This message has been edited by Marilyn deQueiroz (edited October 24, 2001).]
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

There's a *return* statement in catch block, when exception is thrown and caught, just before returning, "only" finally block is executed, and not the code after that. Hence 134 for foo(0), and 23 for foo(1); makes 13423.
HTH,
- Manish
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Because of the "return" in the catch which executes before it gets to the last line (output +="4" ;).
[This message has been edited by Marilyn deQueiroz (edited October 24, 2001).]


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
before the return on catch() the finally() get to run
and then the return in catch() returns back to the main() method.
but if it is System.exit() instead of return.
They fianlly() couldn't run. The program would just exit.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
calanthe
When you call foo the second time with the value of 1 as its argument it throws the exception. In the catch statement you add the '2' to the output and then before the return statement the finally block is executed which adds the '3' to the output. After the finally block is done then the rest of the catch is finished - namely, the return statement. So foo returns before the last line, that adds the '4', is executed.
hope that helped

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform


Dave
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hello wei
This example shows perfectly the way catch finally work:
After executing "output +="2";" a return is going to be executed, but stop, you know that finally must be executed under (almost) any circumstances. And if return were executed the method would return without doing it. So before return is executed the finally clause it is. Afer this the execution of finally return in the catch block is resumed so 4 is no added.
Think finally as a little subroutine that is called before any possible outing way of try/catch code, and after the completion of finally control is returned to the next instruction that was going to be executed.


SCJP2. Please Indent your code using UBB Code
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
hey wei you can see how quick ranchers are...
5 anwsers in 6 minutes

HanMing Low
Ranch Hand

Joined: Oct 18, 2001
Posts: 196
Hi,
I have some doubt on exception that I need some help.
Referring to the above problem, then in what kind of scenario will the output 4 be omitted if the return statement is not there.
What I mean is that when the return statement is commented out, the output+=4 will execute irregardless of whether the exception is thrown or not.
Then, in what case, will the finally excecute but not output+=4
Thanks for any advice.

Han Ming
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

Originally posted by HanMing Low:
I have some doubt on exception that I need some help.
Referring to the above problem, then in what kind of scenario will the output 4 be omitted if the return statement is not there.
What I mean is that when the return statement is commented out, the output+=4 will execute irregardless of whether the exception is thrown or not.
Then, in what case, will the finally excecute but not output+=4

If the exception thrown is unknown/unhandled, and not caught by the catch block, then finally block will execute but not the code after that.
HTH,
- Manish
Nisheeth Kaushal
Ranch Hand

Joined: Jul 20, 2001
Posts: 87
hey friends,
I have a very small doubt, see
output is set to " ", then "i" gets the value 0, and then this 0(int) is converted to string when system calls its toString() method. That means output now have a value of "0" and then we add 1 to this
"0" + 1 so it shud be "0134" why it is "134" ?.
Please reply urgently.
Nisheeth.
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

Originally posted by Nisheeth Kaushal:
hey friends,
I have a very small doubt, see
output is set to " ", then "i" gets the value 0, and then this 0(int) is converted to string when system calls its toString() method. That means output now have a value of "0" and then we add 1 to this
"0" + 1 so it shud be "0134" why it is "134" ?.
Please reply urgently.
Nisheeth.

Have look at the code, carefully.
1) output is set to "", not " " (no space).
2) i is never getting appended to the String output.
Try running the code, that always helps.
HTH,
- Manish

Nisheeth Kaushal
Ranch Hand

Joined: Jul 20, 2001
Posts: 87
I got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about exception
 
Similar Threads
finally doubt
Exception
exception..try catch block execution
Doubt in Exceptions.
Exception