• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Exception

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Is it possible that the program coninues running after the Exception thrown?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you would do something like:
 
umut uzumcu
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic