哪里错了?(异常处理)

Art*_*urV 1 java exception

我无法理解为什么我的代码会捕获异常..我认为构造函数有问题,但我没有看到确切的位置..

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';
    }
}
Run Code Online (Sandbox Code Playgroud)

在主要方法中,我使用以下内容:

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

    catch(Exception e)
    {
        System.out.println(e);
        System.exit(0);
    }
Run Code Online (Sandbox Code Playgroud)

和异常类:

public class IllegalDateFormatException extends Exception
{
    public IllegalDateFormatException(String error) 
    {
        super(error);
    }
}
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助!

dka*_*zel 7

抛出异常的原因是你的大if/else块中有一个bug.

(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
Run Code Online (Sandbox Code Playgroud)

string.charAt(in)返回一个字符.您正在检查值是否为整数.由于字符可以表示为整数值(如ASCII值等),因此表达式date.charAt(1) > 1将始终为真,因为可打印字符以ASCII值32开头

将其更改为单引号

(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'
Run Code Online (Sandbox Code Playgroud)

在未发布的注释中,在检查条件之前创建Exception对象不是一个好主意.创建异常可能很昂贵,因此除非您需要抛出它,否则不要浪费CPU时间.这个更好:

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

 this.date=date;
Run Code Online (Sandbox Code Playgroud)