• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception  RSS feed

 
umut uzumcu
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Is it possible that the program coninues running after the Exception thrown?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes -- if the exception is caught, or if there are other threads besides the one that was terminated by the exception. Do you know how to catch an exception?
 
umut uzumcu
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void main(String args[]) throws InvalidRecord
{

String input=JOptionPane.showInputDialog("\n\tEnter the date of the registration..");


int date=0;

try{
date=Integer.parseInt(input);

}catch(NumberFormatException nfe)
{
throw new InvalidRecord("Date must be numeric");
}


VehicleRecord vr=new VehicleRecord("bnh",date,"ford","escord","colour");

}


for example here, the exception is thrown is user enter non numeric data, and then program stops working, how can i continue coding? for exmaple how can i make user to enter the data again?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're catching the NumberFormatException, but then you throw a InvalidRecord exception. This is what causes your program to exit. Remove the throwing of the InvalidRecord exception and then put the whole thing in a loop.
Something like.

[ March 01, 2007: Message edited by: Joanne Neal ]
 
umut uzumcu
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal,
yes your code is working continuously but it doesnt give any exception error, so its working like a plain while loop. I need to throw an exception and give user an error message like "date should be numeric" and allow them to try again.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you don't need to throw an exception -- you need to display a message. Those are two entirely different things!



If, as a programmer, you want to see the stack trace associated with an exception without stopping the program, just call the "printStackTrace()" method:

 
umut uzumcu
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont know why, its not working in my code, the printStackTrace();

gives me error "cannot resolve symbol".
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by umut uzumcu:
I dont know why, its not working in my code, the printStackTrace();

gives me error "cannot resolve symbol".


Check your spelling. If it still looks alright, then post the exact error message you're getting and the section of code that the error refers to.
 
umut uzumcu
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void validateDate(String date) throws InvalidRecord
{
int date=0;

try{
date=Integer.parseInt(input);

}catch(Exception e)
{
throw new InvalidRecord("Date must be numeric");

}
}

public class InvalidRecord extends Exception
{
InvalidRecord(String invalid)
{
super(invalid);
}
}

For example on the code above I have to have the class called InvalidRecord and specify the exact error for all methods and different errors. and my code should continue after throwing the exception, it shouldnt terminate according to my project.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you would do something like:
 
umut uzumcu
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Garrett Rowe,


Yes i have to use validation methods as well, but here is the other problem, my validation methods shouldnt have return value, so how
the method will know was it valid or not if there is nothing turning
back to method? Validation method will be used with methods in the same class as well as from the other clasess.
Is anybody know how can i get the value from a method without returning it?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
umut --

Look st the code. Garret's proposed "validate()" method returns the argument if the date was valid, and throws an exception if it was not. If the exception is thrown, then we go in the catch block, print a message, and start over. If no exception is thrown, we set a flag which terminates the loop, and the program continues.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!