Results 1 to 6 of 6
  1. #1
    glhansen is offline Member
    Join Date
    Mar 2009
    Posts
    20
    Rep Power
    0

    Default Why can't I catch an exception?

    I've been converting between dates and strings with the form "03/18/09". The code splits the string at the /'s, converts to integers, and sticks it in an instance of Calendar. There are a few different ways for that to go wrong: non-numbers, not enough numbers, and not a valid date. So I've been using try/catch blocks so that parseInt() and Calendar.set() can throw whatever they want, and I just throw my own NumberFormatException if I don't have enough numbers. The exception handling is to refocus on the date field with the text selected, so the user can try again. And that works fine with non-numbers or too few numbers.

    But I get runtime errors vomiting on the screen when I try an invalid date, even from a try/catch block. That part of the code (not including diagnostic System.out.println()s) looks something like this.

    Java Code:
    		try {
    			DateString.StringToCal(ds, Database.date);
    		}
    		catch (Exception ex) {
    			dateText.requestFocus();
    		}
    I thought that would catch any exception that can be thrown. What's up with that?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    try catching Throwable, but I have the feeling that something is being thrown in a spot where you are not expecting it, and so not catching it.

    P.S., what is wrong with SimpleDateFormat?

    SimpleDateFormat (Java Platform SE 6)

    And don't forget about setting
    Java Code:
    sdf.setLenient(false);

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    If an Exception is being thrown in a different thread, a try/catch will not help. You need to thoroughly check for invalid dates before submitting them to your database to prevent this.

  4. #4
    glhansen is offline Member
    Join Date
    Mar 2009
    Posts
    20
    Rep Power
    0

    Default

    I didn't use a SimpleDateFormat because I couldn't figure it out, couldn't figure out how to use it to set a Calendar. I think I've figured it out now, setTime() will set the date, too. But when I parse a date, set the calendar, and get it back out, like

    Java Code:
    		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
    		Date d = new Date();
    		try {
    			d = sdf.parse("03/16/09");
    			c.setTime(d);
    		}
    		catch (Exception e) {System.out.println("Error -- " + e.toString()); }
    		System.out.println("Month: " + c.get(Calendar.MONTH));
    		System.out.println("Day: " + c.get(Calendar.DAY_OF_MONTH));
    		System.out.println("Year: " + c.get(Calendar.YEAR));
    is it normal for the month I get out to be one less than the month I get in? If I put "03/16/09" in, I get
    Java Code:
    Month: 2
    Day: 16
    Year: 2009
    out.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Month is a zero based field (i.e. January is 0 not one). Use another SimpleDateFormat (or the same one) to format the output.

  6. #6
    glhansen is offline Member
    Join Date
    Mar 2009
    Posts
    20
    Rep Power
    0

    Default That was confusing.

    I think I've figured it out. But I saw so many deprecated Date constructors I didn't know if I was even supposed to be using Dates any more, I wasn't sure if I should be storing my dates as Dates or Calendars, I didn't realize that

    StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos)

    could take a single parameter and fill a String, didn't find code examples that made sense to me... so much of this just isn't obvious to someone who's been working with Java for two weeks.

Similar Threads

  1. try catch exception
    By soxfan714 in forum New To Java
    Replies: 3
    Last Post: 05-05-2011, 12:11 PM
  2. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 11:54 AM
  3. try catch...
    By MarkWilson in forum New To Java
    Replies: 8
    Last Post: 06-27-2008, 06:39 PM
  4. try catch!?
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 01-28-2008, 08:51 PM
  5. Try Catch
    By Renegade85 in forum New To Java
    Replies: 4
    Last Post: 12-03-2007, 05:10 PM

Posting Permissions

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