aspose file tools*
The moose likes Beginning Java and the fly likes Runtime exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Runtime exceptions" Watch "Runtime exceptions" New topic
Author

Runtime exceptions

Eric Luedtke
Greenhorn

Joined: Jan 22, 2001
Posts: 2
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
Donald King
Greenhorn

Joined: Dec 08, 2000
Posts: 16
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

Joined: Jan 22, 2001
Posts: 2
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
Janna Lockhart
Greenhorn

Joined: Dec 21, 2000
Posts: 24
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).


- Janna
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

some runtime exceptions are beyond your control.
this doesnt really answer your question, but this is how I handled a situation in my servlet.


SCJP
Visit my download page
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Oct 21, 2000
Posts: 4347
    
    2

just testing new profile signature.
------------------
Dont blindly believe everything I say.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime exceptions