| Author |
problem with try catch
|
naved momin
Ranch Hand
Joined: Jul 03, 2011
Posts: 675
|
|
what i want is when the marks are above 100 then catch clause should be executed
code is
output is
|
The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
naved momin wrote:what i want is when the marks are above 100 then catch clause should be executed
Then add an else statement that throws a NumberFormatException, but it would be better to just add an else statement that printed the error message.
|
Joanne
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32651
|
|
You will only get a NumberFormatException from that code if you enter something like 1.23 or Campbell. If what you enter is a valid integer, you will not get an Exception.
What you want is an IllegalArgumentException.
But you ought not to throw an Exception and catch it in the same method. Rather than doing that, you want an if-else, preferably wrapped inside a loop.
And why are you not using a Scanner, nearly 7 years after it was introduced?
|
 |
Piyush Joshi
Ranch Hand
Joined: Jun 10, 2011
Posts: 207
|
|
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
|
Piyush
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32651
|
|
|
No, lost to Joanne.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32651
|
|
|
. . . and what are you doing if a negative mark is entered?
|
 |
naved momin
Ranch Hand
Joined: Jul 03, 2011
Posts: 675
|
|
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
my refined code is
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 Joshi
Ranch Hand
Joined: Jun 10, 2011
Posts: 207
|
|
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.
|
 |
naved momin
Ranch Hand
Joined: Jul 03, 2011
Posts: 675
|
|
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.
but now whenever i entered a string like fortyfive which could be possible input so this should be catch by catch clause how to do that , now it is giving me a runtime exception of numberformateception ?
runtime exception is
|
 |
Piyush Joshi
Ranch Hand
Joined: Jun 10, 2011
Posts: 207
|
|
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.
|
 |
naved momin
Ranch Hand
Joined: Jul 03, 2011
Posts: 675
|
|
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.
a million thanks !
|
 |
 |
|
|
subject: problem with try catch
|
|
|