Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Runtime exceptions  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
The book I am reading for the exam states that runtime exceptions should never arise in a correct program. It also suggests that it is poor style to use runtime exceptions as deliberate flow control (my take - don't use try/catch statements with runtime exceptions).
That being said, I am trying to convert a string from the command line into an int with the command:
int theNumber = Integer.parseInt( args[0] );
This code works fine when args[0] equals an integer value. However, it fails when you try to pass a float.
My question is how do I handle the situation in order to give the user a meaningful message?
Thanks
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand the issue with try/catch statements. If the Integer class is going to throw an exception when a non-integer is passed in, you ought to catch it.
Try the following code...
public class Test
{
public static void main( String args[] )
{
try
{
int theNumber = Integer.parseInt( args[0] ) ;
}
catch ( Exception e )
{
System.out.println( "Please enter a valid number" ) ;
}
}
}
 
Eric Luedtke
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying Donald. The sample code you provided will work, however the book I am studying from ("The complete Java 2 certification study guide") says that runtime exceptions describe program bugs. It later states that runtime exceptions should never arise in a correctly coded program.
It is also discussed further here: http://www.javaranch.com/ubb/Forum33/HTML/000531.html
This all makes sense to me.
I guess the question I'm asking is can I code for that situation without resorting to try/catch?
Thanks
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the argument is practical that try catch statements should not be used for this reason: Java is an OO language that allows for many different programs, classes, whatever to interact. Any good OO implementation dictates that any class that I write should not have to know the details of any class that it interacts with. That is what OO is all about. Therefor, when I am coding a class, I should not (and maybe cannot) have to look at all the possible exceptions that could occur when interacting with another class. I should just be aware that an exception has the possibility of occuring, and catch it(doing whatever is necessary with that exception when it occurs).
 
Ranch Hand
Posts: 4702
9
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
some runtime exceptions are beyond your control.
this doesnt really answer your question, but this is how I handled a situation in my servlet.

 
Randall Twede
Ranch Hand
Posts: 4702
9
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm that didnt works so well did it. If they enter a non-number I send them an error page telling them what they did wrong. If I didnt, they would get a default error page from the server that just says NumberFormatException etc etc

[This message has been edited by Randall Twede (edited January 23, 2001).]
 
Randall Twede
Ranch Hand
Posts: 4702
9
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just testing new profile signature.
------------------
Dont blindly believe everything I say.
 
straws are for suckers. tiny ads are for attractive people.
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!