Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with try catch

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i want is when the marks are above 100 then catch clause should be executed
code is

output is

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, lost to Joanne.
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and what are you doing if a negative mark is entered?
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic