• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

problem with try catch

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 75659
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 75659
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, lost to Joanne.
 
Campbell Ritchie
Marshal
Posts: 75659
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic