Results 1 to 2 of 2
  1. #1
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default 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);
    	}
    }
    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");
    			}
    	}
    }
    Your help is appreciated.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default

    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

  1. Replies: 6
    Last Post: 12-09-2010, 08:44 AM
  2. writing Custom exception and its implementation
    By vinsun in forum Advanced Java
    Replies: 1
    Last Post: 07-02-2010, 10:24 AM
  3. [SOLVED] SWT Custom Exception handling
    By shinobu in forum SWT / JFace
    Replies: 2
    Last Post: 04-21-2009, 09:39 AM
  4. Compiling and using jar file for custom classes
    By MAILMIRZA in forum New To Java
    Replies: 3
    Last Post: 01-12-2009, 04:56 PM
  5. How to use <,>,== on custom classes
    By Bojevnik in forum Advanced Java
    Replies: 4
    Last Post: 10-29-2007, 05:00 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
  •