Results 1 to 2 of 2
Thread: Custom Exception Classes
- 04-04-2011, 09:46 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 38
- Rep Power
- 0
Custom Exception Classes
I am having trouble with creating my own custom Exception Classes. I have kind of got this one to work but im finding it will only pull up one error. I have to use the class, as is for the Exception class. I have written my own tester, thats where there is a problem. As soon as it finds one error it fails to move on and check for the next.
Java Code:public class InvalidDataException extends Exception { InvalidDataException(String ms) { super(ms); } }Your help is appreciated.Java Code:public class DateTester { public static void main(String[] args) { String date = "01|42|3001"; String[] temp = null; temp = date.split("|"); try { int day = Integer.parseInt(temp[1] + temp[2]); DateTester.checkDay(day); int month = Integer.parseInt(temp[4] + temp[5]); DateTester.checkMonth(month); int year = Integer.parseInt(temp[7] + temp[8] + temp[9] + temp[10]); DateTester.checkYear(year); System.out.println(day + " " + month + " " + year); } catch(NumberFormatException ex) { System.out.println("Sorry invalid numbers."); } catch(InvalidDataException ex) { System.out.println(ex); } } public static void checkDay(int day)throws InvalidDataException { if (day < 00 || day > 31) { throw new InvalidDataException("Invalid day"); } } public static void checkMonth(int month)throws InvalidDataException { if (month < 00 || month > 12) { throw new InvalidDataException("Invalid month"); } } public static void checkYear(int year)throws InvalidDataException { if (year > 2010 || year < 2000) { throw new InvalidDataException("Invalid year"); } } }
- 04-04-2011, 11:02 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
As soon as an exception is thrown inside the try/catch block then control moves to the catch part. That's how these things work.
So in your case, as soon as an exception is thrown then you print the exception and that's the end of your program.
If you want everything saved up, then you either stick try/catch around each check, or you somehow build up a list of errors which you then display at the end.
Similar Threads
-
Custom exception exits program, while NumberFormatException only shows error
By DerekRaimann in forum New To JavaReplies: 6Last Post: 12-09-2010, 08:44 AM -
writing Custom exception and its implementation
By vinsun in forum Advanced JavaReplies: 1Last Post: 07-02-2010, 10:24 AM -
[SOLVED] SWT Custom Exception handling
By shinobu in forum SWT / JFaceReplies: 2Last Post: 04-21-2009, 09:39 AM -
Compiling and using jar file for custom classes
By MAILMIRZA in forum New To JavaReplies: 3Last Post: 01-12-2009, 04:56 PM -
How to use <,>,== on custom classes
By Bojevnik in forum Advanced JavaReplies: 4Last Post: 10-29-2007, 05:00 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks