Results 1 to 6 of 6
Thread: Why can't I catch an exception?
- 03-14-2009, 09:54 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 20
- Rep Power
- 0
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.
I thought that would catch any exception that can be thrown. What's up with that?Java Code:try { DateString.StringToCal(ds, Database.date); } catch (Exception ex) { dateText.requestFocus(); }
- 03-14-2009, 11:51 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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);
- 03-15-2009, 08:04 AM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.
- 03-16-2009, 09:28 PM #4
Member
- Join Date
- Mar 2009
- Posts
- 20
- Rep Power
- 0
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
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 getJava 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));
out.Java Code:Month: 2 Day: 16 Year: 2009
- 03-16-2009, 09:51 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Month is a zero based field (i.e. January is 0 not one). Use another SimpleDateFormat (or the same one) to format the output.
- 03-17-2009, 06:48 PM #6
Member
- Join Date
- Mar 2009
- Posts
- 20
- Rep Power
- 0
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
-
try catch exception
By soxfan714 in forum New To JavaReplies: 3Last Post: 05-05-2011, 11:11 AM -
how to catch two exceptions in one catch()?
By arnab321 in forum New To JavaReplies: 1Last Post: 11-06-2008, 10:54 AM -
try catch...
By MarkWilson in forum New To JavaReplies: 8Last Post: 06-27-2008, 05:39 PM -
try catch!?
By Joe2003 in forum Advanced JavaReplies: 2Last Post: 01-28-2008, 07:51 PM -
Try Catch
By Renegade85 in forum New To JavaReplies: 4Last Post: 12-03-2007, 04:10 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks