Results 1 to 2 of 2

Thread: Exception handling help (newbie)

  1. #1
    Join Date
    Dec 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception handling help (newbie)

    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:

    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)

    Code:
    // 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:

    Code:
    	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.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    A method that throws an exception of type SomeException looks like this:
    Code:
    void someMethod() throws SomeException {
        throw new SomeException();
    }
    And to catch that exception, one would do:
    Code:
    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:
    Code:
    void someMethod() throws SomeException, SomeOtherException {
        throw someCondition ? new SomeException() : new SomeOtherException();
    }
    Code:
    try {
        someMethod();
    } catch (SomeException e) {
        // handle a SomeException
    } catch (SomeOtherException e) {
        // handle a SomeOtherException
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •