【发布时间】:2013-11-14 01:48:11
【问题描述】:
创建类TestBook,并创建测试用例来测试类Book
在这个阶段不要处理任何异常,只需将它们传递给调用者方法。
上面的指令是否意味着我的测试器类,TestBook 应该有它的主要方法“抛出 ParseException”,当我运行我的程序时出现“错误”,这不应该崩溃,而是应该将异常传递给...?
public class TestBook{
public static Book directory=new Book(); //Book contains an arraylist of dates
public static void main(String[] args) throws ParseException {
directory.addApt("01-04-1996","testdate");
//this will create an instance of a Apt and is in the INCORRECT format.
}
Book 类的 addApt 方法如下所示:
String[] dates=date.split("-");
if(dates[0]!=2)
throw new ParseException("incorrect format day should be in numbers,2);
if(dates[0]!=3)
throw new ParseException("incorrect format month should be in letters,2);
if(dates[0]!=4)
throw new ParseException("incorrect format year should be in numbers,2);
Apt newAppt=new Apt=(date,type);
当我运行它时,我收到一条错误消息:线程“main”java.text.ParseException 中的异常:格式不正确,月份应该是字母。
但我的问题是为什么会出现这种情况,既然我正在扔它,为什么它会这样处理它?我想我对 throw(s) vs. try/catch 感到困惑..
【问题讨论】:
-
你已经声明从 main 抛出 ParseException 而不处理它。
-
我很确定这段代码不能编译。你的意思是
dates[0].length != 2?那么应该是dates[0]、dates[1]等