aspose file tools*
The moose likes Beginning Java and the fly likes Exceptions stoping my program. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exceptions stoping my program." Watch "Exceptions stoping my program." New topic
Author

Exceptions stoping my program.

Rik Ny
Greenhorn

Joined: Oct 13, 2001
Posts: 13
Hello there, I have written a program that gets input from the user and then prints it to a file. However because I am using BufferedReader i have had to use a try and catch block. For the input i have placed a catch block which catches any invalid values. However when the program runs, and the user enters a invalid value it shows the exception which I want. But is there any way that I can make the program continue once a exception has been caught??? I.e. once my program catches an exception it terminates the entire program. Cheers, Rik
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
It sounds like you may be catching the wrong exception. If you don't catch the exception throw, it trickles up to the default exception handler which stops the program and prints the stack trace. Make sure your catch phrase catchs the appropriate exception. If you catch it, you don't need to do anything else, your catch block can be empty, which essentially is telling the program to catch the exception and do nothing about it.
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud


Michael J Bruesch<br /><i>I code, therefore I am.</i>
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
Like michael said, your program should have continued right after the exception is caught, if the right exception was caught. If you want to continue where the exception was thrown, i.e. you want to continue retrirving input after handling invalid data, then you have to put your try/catch within some kind of loop. It's up to you to choose what will signal the end of the loop.

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Rik Ny
Greenhorn

Joined: Oct 13, 2001
Posts: 13
Thanks, but here is my try catch block. Am I using the write Exception???
protected void UserResponse(BufferedReader responseReader)
{
try
{
response =Integer.parseInt(responseReader.readLine());
}
catch(Exception ex)
{
System.out.println("ERROR:- " + ex);
}
}
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

I don't have my books with me at the moment but I think you want to catch "NumberFormatException".


Please ignore post, I have no idea what I am talking about.
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
It should still work using Exception there, but Christopher is right, you should try to catch the most specific exception if it's not known, in this case, I do believe it would be NumberFormatException. But yours should still work, so I don't know what the problem is.
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
This question may be idiot, but are you sure that your program really zxits? What happens with your variable "response" when an exception is raised? What is its default value? Do you test this value after the exception block?
W.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions stoping my program.
 
Similar Threads
Try / Catch Statements
Exceptions
empty try block
Exception Handling - Exception Subclasses
Another Question