Java try catch()问题

来源:百度知道 编辑:UC知道 时间:2024/05/25 14:18:38
首先定义了一个类,
public class Vehicule{
private String s;
private Date da=d;
public Vehicule(String s,Date d)throws Exception
{this.s=s;
if(d.compareTo(new Date())>0)
throw new Exception("date incorrect");
this.d=d;
}
````````````}
然后建立一个子类
public class Car extends Vehicule{
private int a;
public Car(String e,Date s,int a)throws Exception
{super(e,s);
this.a=a;
}
``````````}
然后调用
public class Application{
public static void main(String[] args) {
Car a;
int s;
String w;
Date ff;
try{
a=new Car(w,ff,s);
}
catch(Exception aaa)
{System.out.println(aaa.getMessage());
System.exit(-1);}
}
}
运行之后,无论输入的日期在当前时间前还是后,最后都会抛出错误“date incorrect”这是为什么呢 ?
谢谢。
首先定义了一个类,
public class Vehicule{
private String s;\\车名
private Date da; \\生产日期
public Vehicule(String s,Date da)throws Ex

我帮你改了一下程序,你可以试运行一下
import java.util.Date;
import java.util.Calendar;
public class DateTest{
private String s;
private Date da;
public DateTest(String s,Date d)throws Exception
{this.s=s;
if(d.compareTo(new Date())>0)
throw new Exception("date incorrect");
this.da=d;
}
protected Date getDate(){
return da;
}
public static void main(String[] args) {
Car1 a,b;
int s=0;
String w=null;
Date ff=new Date();
Calendar c=Calendar.getInstance();
c.setTime(ff);
c.add(Calendar.MONTH,1);
try{
b=new Car1(w,new Date(),s); //日期相等
System.out.println(b);
a=new Car1(w,c.getTime(),s); //日期大于当前日期
System.out.println(a);
}
catch(Exception aaa)
{System.out.println(aaa.getMessage());
System.exit(-1);}
}
}
//然后建立一个子类
class Car1 extends DateTest{
private int a;
public Car1(Str