The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
naved momin wrote:what i want is when the marks are above 100 then catch clause should be executed
Joanne
naved momin wrote:what i want is when the marks are above 100 then catch clause should be executed
Piyush
Piyush Joshi wrote:
naved momin wrote:what i want is when the marks are above 100 then catch clause should be executed
This is not an appropriate use of NumberFormatException.
NumberFormatException should be "Thrown to indicate that the application has attempted to convert
a string to one of the numeric types, but that the string does not have the appropriate format."
Instead of this your logic can be implemented by writing an else block to your if block. For Example:
Oh!! lost to Campbell
The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Campbell Ritchie wrote:No, lost to Joanne.
Campbell Ritchie wrote:. . . and what are you doing if a negative mark is entered?
Piyush
Piyush Joshi wrote:
Campbell Ritchie wrote:No, lost to Joanne.
Did'nt see that one also!!
Campbell Ritchie wrote:. . . and what are you doing if a negative mark is entered?
Its upto naved.
The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
naved momin wrote:
sorry what i want to do is when some one entered float value or when some one entered something like fortyfive for marks then this should be catch by catch clause ....i m learning try catch module from my book , so i m not a experienced programmer or so
can you tell me what should be done for that ?
Piyush
Piyush Joshi wrote:
naved momin wrote:
sorry what i want to do is when some one entered float value or when some one entered something like fortyfive for marks then this should be catch by catch clause ....i m learning try catch module from my book , so i m not a experienced programmer or so
can you tell me what should be done for that ?
Do you understand why NumberFormatException or for that matter some exception is thrown?
Its because of some error for example: invalid arguments passed to methods.
Next you need to understand What does the following code do:
b.readLine() reads whatever user entered and returns it as a String.
and then this String is passed to parseInt() method in Integer class. If you read the javadocs for this method
then you can see that this method throws NumberFormatException if if the string does not contain a parsable integer.
Therefore if some one enters float value or something like fortyfive and when you parse it to Integer then a NumberFormatException will be thrown.
Now If you want to handle this exception i.e. you want to do something like print an error msg then you should enclose the above method calls in a try block and write a following catch block catching NumberFormatException, like this:
I hope it answers your doubts.
The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/