简体   繁体   中英

Where I am wrong? (Exception handling)

I can't get why my code catches exception.. I assume there is something wrong with a constructor, but I don't see where exactly..

public class OrderDate 
{
    private String date;

    public OrderDate(String date) throws IllegalDateFormatException
    {   
        IllegalDateFormatException wrongDate = 
                new IllegalDateFormatException("Date must have the following"
                                                        + " format: dd/mm/yy");
        if(date.length() > 8        
                || (date.charAt(0) == 0 && date.charAt(1) == 0)
                || (date.charAt(3) == 0 && date.charAt(4) == 0)
                || (date.charAt(0) == 3 && date.charAt(1) > 1)
                || (date.charAt(3) == 1 && date.charAt(4) > 2)                
                ||  date.charAt(2) != '/'
                ||  date.charAt(5) != '/'
                ||  date.charAt(0) > 3
                ||  date.charAt(3) > 1
                ||  !isDigit(date.charAt(0))
                ||  !isDigit(date.charAt(1))
                ||  !isDigit(date.charAt(3))
                ||  !isDigit(date.charAt(4))                        
                ||  !isDigit(date.charAt(6)) 
                ||  !isDigit(date.charAt(7)))                        

            throw wrongDate;
        else
            this.date = date;
    }

    private boolean isDigit(char z)
    {
        return z >= '0' && z <= '9';
    }
}

In the main method I use the following:

    try
    {
        OrderDate myDate = new OrderDate("10/02/15");
        System.out.println("all fine");
    }

    catch(Exception e)
    {
        System.out.println(e);
        System.exit(0);
    }

And the exception class:

public class IllegalDateFormatException extends Exception
{
    public IllegalDateFormatException(String error) 
    {
        super(error);
    }
}

Thanks very much for help!

The reason you are throwing an exception is your large if/else block has a bug in it.

(date.charAt(0) == 0 && date.charAt(1) == 0)
            || (date.charAt(3) == 0 && date.charAt(4) == 0)
            || (date.charAt(0) == 3 && date.charAt(1) > 1)
            || (date.charAt(3) == 1 && date.charAt(4) > 2)
     ...
            || date.charAt(0) > 3
            ||  date.charAt(3) > 1

string.charAt(in) returns a character. you are checking to see if the values are ints. Since characters can be represented as integer values (like ASCII value etc) expressions like date.charAt(1) > 1 will always be true since printable characters start as ASCII value 32

Change it to single quoted values

(date.charAt(0) == '0' && date.charAt(1) == '0')
            || (date.charAt(3) == '0' && date.charAt(4) == '0')
            || (date.charAt(0) == '3' && date.charAt(1) > '1')
            || (date.charAt(3) == '1' && date.charAt(4) > '2')
            ...
            || date.charAt(0) > '3'
            ||  date.charAt(3) > '1'

On an unreleated note, it's not a good idea to create the Exception object before checking for the condition. Exception creation can be expensive so don't waste the CPU time unless you need to throw it. This is better:

 if(date.length() > 8 ...){
     throw new IllegalDateFormatException("Date must have the following"
                                                    + " format: dd/mm/yy");
 }

 this.date=date;

Using Simpledateformat may make your life a lot easier.

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yy");
    try {
        Date date = simpleDateFormat.parse("15/02/99");
    } catch (ParseException e) {
        throw IllegalDateFormat("Date must have the following format: " +
                "dd/mm/yy");
    }
charAt(someIndex) 

returns a char not an int. You have to check for

== '0'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM