PDA

View Full Version : Exception handling help (newbie)



DHS1
12-28-2008, 12:26 AM
Hey guys. I have a lab that is due in two weeks, but I wanted to start on it now. Problem is, I'm at home during christmas break so I can't ask my professors. Here's my problem:

I am given a very fragile program and I have to add the exception handling and error checking and pretty much make it uncrashable.

I will show just one small step in this lab. If you can answer it, I think I'll be able to do the rest. I just need to get headed in the right direction because my Java book and online tutorials aren't helping.

I need to do what the TODO says. This is a Constructor method. The length it's talking about has to be 10. Here's the code:


public DateStringConverter(String startDate, String endDate ){
//TODO make sure that both text fields contain text; generate an Exception if NULL strings or invalid length;
//TODO In the Exception, specify the reason: exactly which field (Start or End Date) contained the error.

parseDateStrings( startDate, endDate );
}

I'm then supposed to catch the exceptions (that I made in that constructor) in this code (remember Im just showing a small secion)



// prompt for input
String startDate = JOptionPane.showInputDialog("Enter a start date as mm/dd/yyyy.");
String endDate = JOptionPane.showInputDialog("Enter an end date as mm/dd/yyyy");

//TODO catch Exceptions thrown from within DateStringConverter; report errors on exceptions,
//TODO report EXACTLY the cause of the error in a message dialog, and loop to repeat the previous prompts
DateStringConverter dsc = new DateStringConverter(startDate, endDate);

I tried putting exception handling in the constructor method I showed you earlier, and this is what it looked like:


public DateStringConverter(String startDate, String endDate ) throws Exception{
//TODO make sure that both text fields contain text; generate an Exception if NULL strings or invalid length;
//TODO In the Exception, specify the reason: exactly which field (Start or End Date) contained the error.
if((startDate==null)||(startDate.length()>10||startDate.length()<10)){
Exception start = new Exception("If start date is null or too short/long");
throw start;
}
if((endDate==null)||(endDate.length()>10||endDate.length()<10)){
Exception end = new Exception("If end date is null or too short/long");
throw end;
}
parseDateStrings( startDate, endDate );

But when I went to the main method (second block of code I showed you) I couldn't figure out how to catch the exception named "start" that I threw.

So can anyone tell me how I could do this? The TODO:'s in the code are what I need to do in case you didn't read this whole thing. Thanks, and let me know if you need to see more of the code.

Twey
12-28-2008, 03:25 PM
A method that throws an exception of type SomeException looks like this:
void someMethod() throws SomeException {
throw new SomeException();
}And to catch that exception, one would do:
try {
someMethod();
} catch (SomeException e) {
// Handle the exception (which object is available as 'e')
}The types are specified because you can throw and handle multiple types of exception at once:
void someMethod() throws SomeException, SomeOtherException {
throw someCondition ? new SomeException() : new SomeOtherException();
}
try {
someMethod();
} catch (SomeException e) {
// handle a SomeException
} catch (SomeOtherException e) {
// handle a SomeOtherException
}