File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Exception handling questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception handling questions" Watch "Exception handling questions" New topic
Author

Exception handling questions

Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Hi,
Here are my Exception Handling questions:
Q.1 What does "clean up code" when using "finally"? Clean up which code and does it write by the programmer to clean it up?
Q.2 What does "exception propagation" and "call stack" mean?
Q.3 When should use the printStackTrace() and fillInStackTrace()? Why do we need to re-throw the exception?
Q.4 What's wrong of the following program and how can I fix it?

Thanks
Andrew
[This message has been edited by Cindy Glass (edited December 19, 2001).]
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Q1.
The "finally" code will be executed before leaving the method, even if an exception has been thrown. You can put any code you want in this block. But in most cases it makes sense to clean up, as you are about to leave the method.
Q2.
Exception propagation means that if you thrown something it will work it's way back up the "call stack" until it finds someone to catch it.
Call stack is just the order of methods which have been called. Method1 calls Method2 calls Method3, than your call stack would contain:
Method3
Method2
Method1
Q4.
Please use the "code" tags to make your posts easyer to read.
There is nothing wrong with the code you have posted. It is an example of throws, catches, and finally. What do you want the code to do?
Do you need me to explain that the code is doing?


Please ignore post, I have no idea what I am talking about.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
It is sort of strange to throw an exception if you have not yet encountered an exception. One normally would not put a throw statement in the middle of a try block for no good reason. You might consider putting that throw in the catch block - where you will end up when you DO encounter an exception.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 180

In addition: you should initialize 'out' to null and check to see if it is not null before you call 'close'.
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Thanks for all of your kind help.
This is a question from Globalknowledge. It said that the program fails to compile due to an error at "out.close".
And there are 3 errors when I run it by jdk1.3.
The answer is that Any method that throws a checked exception must be called within a try clause, or the method must declare that it throws the exception(Don't understand here!). The finally clause does not count.
Could you tell me how to use code tag, bold and block of code like what you did?
Regards
Andrew
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Sorry, I forgot to ask:
Exception propagation means that if you thrown something it will work it's way back up the "call stack" until it finds someone to catch it.
If there is no method to catch it, what will the result? Pass to main method? If it is not handled by main method, what will it be?
Thanks a lot.
Andrew

Colin Kenworthy
Ranch Hand

Joined: Aug 06, 2001
Posts: 88
Originally posted by Andrew Parker:
Any method that throws a checked exception must be called within a try clause, or the method must declare that it throws the exception(Don't understand here!).

Basically the above says that you MUST do something with the exception if it is raised. The exception can be dealt with in a catch block or if you don't want to deal with it you must declare that you will throw the exception back up the method calling chain.
If you throw the exception, then now everything that calls your method needs to do it within a try/catch block!
See http://www.javaranch.com/ubb/ubbcode.html for how to use bold and formatting code examples.
The final catcher for exceptions is the JVM. You will see this when you get a run time exception.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986


Ok the first compile error is telling you that because you have a "throw" before the last line of code in the try block, the last line of code in the try block with never be reached.
Not so sure about the second one.
The third error is telling you the "close" method has a exception which needs to be caught or delared. It is your choice. So if you go to the API for that method you will find that IOException may be thrown in the event that there are problems closing the FileOutputStream. So you can either 1] place that code within a try block and have a corresponding catch block for IOException, or 2] say "throws IOException" following the method signiture.

[This message has been edited by christopher foran (edited December 20, 2001).]
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Hi,
Someone helped me to fix the bug:

I have not seen that the try catch block can be placed inside the finally block before.
Thanks for all of your help.
Andrew

[This message has been edited by Cindy Glass (edited December 21, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception handling questions