Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions stoping my program.

 
Rik Ny
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rik Ny
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have my books with me at the moment but I think you want to catch "NumberFormatException".
 
Michael Bruesch
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic