Exception handling throw and throws
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
throw is used to throw an exception manually where as throws is used in the case of checked exceptions to reintimate the compiler that we have handled the exception. so throws is to be used at the time of defining a method and also at the time of calling that function which rises an checked exception.
if we want to rise our own exception we have to use either throws or to handle the exception by try-catch. if not it gives the compile time error.
and throw is to rise the exception manually In the above prog. I rised an exception when you try to divide a no with 1.(own Exception)
N.Senthil Kumar
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Example:
HTH
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Output:-
This is a Kathy S book example it says that it will not compile.....
MyException.java3:reported exception MyException; must be caught or declared to be thrown
doStuff();
So in order to execute this program how can we actually fix it or implement throws clause....
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I read your program and Kathy S book....The book says that in the program code of yours if it throws a checked exception but you are not declaring it...
It goes followed by the following example:-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Sonali Sehgal wrote:Thanks a lot Sagar and Ankit ...but I still have a question
Output:-
This is a Kathy S book example it says that it will not compile.....
MyException.java3:reported exception MyException; must be caught or declared to be thrown
doStuff();
So in order to execute this program how can we actually fix it or implement throws clause....
throws clause is used properly in this program. doStuff method throws MyException and has declared it in its throws clause. doStuff is called in someMethod(). But some method neither declared MyException in its throws clause nor catches it. That's why you are getting an error. The two possible solutions for this are
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks a lot now I understand the declaration and rules of throws clause..........I am now able to understand all the above programs........
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Sonali Sehgal wrote:Hi Sagar,
I read your program and Kathy S book....The book says that in the program code of yours if it throws a checked exception but you are not declaring it...
It goes followed by the following example:-
Re read my first post, particularly this line:
...then just pass it on to the calling function, and let him handle this exception
That's mean, calling function (doStuff()) must handle the exception which are declared in called functions (doMore()) 'throws' clause.
HTH
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
| There’s no place like 127.0.0.1. But I'll always remember 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 |











