Throwing Exceptions
posted 21 years ago
This code give a compile time error.
Child.java:8: unreported exception java.sql.SQLException; must be caught or decl
ared to be thrown
doStuff();
But If i do
What i want to know is when i have caught the Exception in doStuff() method which is the method that throws it then why do i have to declare it in someMethod() which calls doStuff() method.
Thanks
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
This code give a compile time error.
Child.java:8: unreported exception java.sql.SQLException; must be caught or decl
ared to be thrown
doStuff();
But If i do
What i want to know is when i have caught the Exception in doStuff() method which is the method that throws it then why do i have to declare it in someMethod() which calls doStuff() method.
Thanks
posted 21 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
When you declare a "throws Exception", you force the caller of your method to: 1) catch your Exception OR 2) throw it furter (like your second code block)..
Greetz
Greetz
Angela lewis
Ranch Hand
Posts: 100
posted 21 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks for replying.
What if the caller does both i.e, catch it as well as throw it further.
What if the caller does both i.e, catch it as well as throw it further.
Geoffrey Vlassaks
Greenhorn
Posts: 24
posted 21 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Than the same rules apply to the caller of the second method. However, it's possible that the declared exception is never thrown. But you still have to catch it, or throw it further...
Greetz
Greetz
Angela lewis
Ranch Hand
Posts: 100
posted 21 years ago
The method someMethod() needs to either throw the exception or catch it.
But as in this code it does both.
Does this have any implication or is it same as doing one of the two?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The method someMethod() needs to either throw the exception or catch it.
But as in this code it does both.
Does this have any implication or is it same as doing one of the two?
posted 21 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Doing both doesn't make any sense.You should either catch it or throw it.
Geoffrey Vlassaks
Greenhorn
Posts: 24
posted 21 years ago
In this code, the exceptions are thrown upward. The caller of "anotherMethod()" still has to catch the SQLException, or throw it further. If you don't declare the throws SQLException in "anotherMethod()", you have to catch the exception from "someMethod()"
Hope this helps a bit more..
Greetz
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
In this code, the exceptions are thrown upward. The caller of "anotherMethod()" still has to catch the SQLException, or throw it further. If you don't declare the throws SQLException in "anotherMethod()", you have to catch the exception from "someMethod()"
Hope this helps a bit more..
Greetz
| Always! Wait. Never. Shut up. Look at this tiny ad. Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |









