Forums Register Login

Exception Handling

+Pie Number of slices to send: Send
Hi all!
It was all an easy ride until I reached Exception Declaration and the Public Interface.

Can anybody explain me the stuff here.



Are these two functions ok??? If no, then where is the trouble... do I need to provide a try/catch anywhere, do I need to provide a "throws" for these two methods, if yes, then to which method can i do it, and to which method can I do without them... i would gladly welcome a detailed explaination.
+Pie Number of slices to send: Send
I guess The same example is in K&B book. In do more stuff you are throwing a checked exception. this has to be handled either using try catch or throws clause. If you use throws clause, then method that calls this domore() should also handle this checked exception
+Pie Number of slices to send: Send
 

Originally posted by Srinivasa Raghavan:
I guess The same example is in K&B book. In do more stuff you are throwing a checked exception. this has to be handled either using try catch or throws clause. If you use throws clause, then method that calls this domore() should also handle this checked exception



You are right. It is obviously from K&B and I have read pages 249-250 three times. Still I felt the necessity to discuss it here.

In do more stuff you are throwing a checked exception. this has to be handled either using try catch or throws clause.

Ok, so i must use either try/catch or throws. What if I use both??

And do I need to secure both of my methods??? Can i use try catch for one and throws for the other??? oh someone help me plzzzzzzzzzzzzzzz...
+Pie Number of slices to send: Send
At least, to make the code to compile fine, you have to do one of the following :

option 1:
Use a try / catch in doMore()

option 2:
Add a throws clause in doMore() declaration and add a try / catch block in doStuff()



option 3:
Add a throws clause for doMore() AND for doStuff()
+Pie Number of slices to send: Send
Thanks Seb, browsing page 284-285 from Beginning Java 2 by Ivor Horton, re-reading page 250 from K&B and finally your reply... I end up this way....

Any method which can throw an checked-exception (raised either manually or by the code) has to defend itself. It can either do it by try/catch or by throws.

A try/catch would complety burry the risky thing. So,


1. If the method (doMoreStuff in this case)does by try/catch, then other methods that call it (doStuff in this case)will have no trouble.

2. But if the method (doMoreStuff) simply defends itself by declaring a "throws", then it will escape, however other methods (like doStuff) that call this risky method, will have to defend themselves separately, they can again either chose to supply a "throws" clause or provide the "try/catch" and burry the risk-factor there.

Correct me if I am heading on wrong side...
[ September 28, 2005: Message edited by: Akhil Trivedi ]
+Pie Number of slices to send: Send
That is correct, and that is only for checked exceptions.

In the case of checked exception, if you are going to use throws, the method signature should also bear the exception name or the super class of the exception.

if you use try-catch block, then the method signature need not bear the exception name.

Also this is applicable only for checked exception...

in your code if doMore throws NumberFormatException, which is a run-time exception, you do not need to explicilty mention it in the method name.
+Pie Number of slices to send: Send
Akhil , you are on the right track.
I knew that guy would be trouble! Thanks tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 746 times.
Similar Threads
Instance variable vs method argument
Question about wait() method
wait() method question
Chapter 5 of SCJP Java 6 book of Kathy Sierra/Bert Bates
Query: Rethrowing the Same Exception - Kathy Sierra
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:26:46.