public static void main(String[] args) { String[][] t = new String[13][6]; t[0] = new String[]{"悉尼","13512345678","2023-06-12","2023-06-20","飞机","2"}; t[1] = new String[]{"上海","1351234567c","2023-06-12","2023-06-20","飞机","2"}; t[2] = new String[]{"上海","1351234567","2023-06-12","2023-06-20","飞机","2"}; t[3] = new String[]{"上海","23512345678","2023-06-12","2023-06-20","飞机","2"}; t[4] = new String[]{"上海","13512345678","2023-06-2","2023-06-20","飞机","2"}; t[5] = new String[]{"上海","13512345678","2023-06-12","2023-06-11","飞机","2"}; t[6] = new String[]{"上海","13512345678","2023-06-12","2023-06-20","飞机","2"}; t[7] = new String[]{"上海","13512345678","2023-06-12","2023-06-20"," ","2"}; t[8] = new String[]{"上海","13512345678","2023-06-12","2023-06-20","自驾","-1"}; t[9] = new String[]{"上海","13512345678","2023-06-12","2023-06-20","火车","2.5"}; t[10] = new String[]{"上海","13512345678","2023-06-12","2023-06-20","自驾","2"}; t[11] = new String[]{"上海","13512345678","2023-06-12","2023-06-20","火车","2"}; t[12] = new String[]{"上海","13512345678","2023-06-12","2023-06-20","飞机","0"}; String[] a={"出差地无效","联系电话无效","联系电话无效","联系电话无效","日期无效", "日期无效","日期无效","出行方式无效","同行人数无效","同行人数无效","提交申请","提交申请","提交申请"}; String expected,actual,result; System.out.println("编号"+"\t"+"出差地"+"\t"+"联系电话"+"\t"+"开始日期"+"\t"+"结束日期"+"\t"+"出行方式"+"\t"+"同行人数"+"\t"+"预期输出"+"\t"+"实际输出"+"\t"+"测试结果"); try{ for(int i=0;i<=12;i++){ expected=a[i]; actual=application(t[i][0],t[i][1],t[i][2],t[i][3],t[i][4],t[i][5]); if(expected.equals(actual)) result="Passed"; else result="Failed"; System.out.println("T["+(i+1)+"]"+"\t"+t[i][0]+"\t"+t[i][1]+"\t"+t[i][2]+"\t"+t[i][3]+"\t"+t[i][4]+"\t"+t[i][5]+"\t"+expected+"\t"+"\t"+actual+"\t"+result);} }catch(ParseException e) { // 处理 ParseException 异常的代码 }如何处理异常
时间: 2023-07-12 16:02:12 浏览: 137
在这个代码中,ParseException 异常是在 application 方法中可能抛出的,因此可以在调用 application 方法的 try-catch 块中处理该异常。具体代码可以修改如下:
```
try{
for(int i = 0; i <= 12; i++){
expected = a[i];
try{
actual = application(t[i][0], t[i][1], t[i][2], t[i][3], t[i][4], t[i][5]);
if(expected.equals(actual))
result = "Passed";
else
result = "Failed";
System.out.println("T[" + (i + 1) + "]" + "\t" + t[i][0] + "\t" + t[i][1] + "\t" + t[i][2] + "\t" + t[i][3] + "\t" + t[i][4] + "\t" + t[i][5] + "\t" + expected + "\t" + "\t" + actual + "\t" + result);
} catch(ParseException e){
System.out.println("T[" + (i + 1) + "]" + "\t" + t[i][0] + "\t" + t[i][1] + "\t" + t[i][2] + "\t" + t[i][3] + "\t" + t[i][4] + "\t" + t[i][5] + "\t" + "ParseException" + "\t" + "\t" + "ParseException" + "\t" + "Failed");
}
}
} catch(Exception e){
System.out.println("Unhandled exception type " + e.getClass().getName());
}
```
在这个代码中,我们在外层添加了一个 try-catch 块来处理所有可能的异常。在内层的 try-catch 块中,我们捕获了 application 方法可能抛出的 ParseException 异常,并在 catch 块中输出测试结果。如果发生其他类型的异常,我们将在外层的 catch 块中输出异常信息。
阅读全文