Exception
checked and unchecked exception
checked exception:要handle的exception,如:找不到xxx
unchecked exception:不handle,直接退出的exception,如NullPointerException
不用handle的是unchecked,用handle的是checked
Exception类是checked exception
RuntimeException和Error类是unchecked exception
Javadoc
checked exception需要注释,unchecked exception和error不需要
/**
* @throws NotPerfectSquareException if x it not a perfect square
*/
int integerSquareRoot(int x) throws NotPerfectSquareException{
...
}
定义和抛出异常
public class NotPerfectSquareException extends Exception {
public NotPerfectSquareException() { super(); }
public NotPerfectSquareException(String message) { super(message); }
public NotPerfectSquareException(String message, Throwable cause) { super(message, cause); }
public NotPerfectSquareException(Throwable cause) { super(cause); }
}
/**
* @throws NotPerfectSquareException if x it not a perfect square
*/
int integerSquareRoot(int x) throws NotPerfectSquareException{
throws new NotPerfectSquareException();
}
Exception的执行
try{
A;
B;
}
catch(Exception1 e1){
C;
}
final{
D;
}
无论怎么执行try和catch,最后都会执行final中的内容
当A结束时出现异常,如果被捕获就执行C,D,如果没被捕获直接执行D然后退出
Test
Test Strategy
白盒测试
针对代码执行路径设计测试,要求全覆盖
黑盒测试
只考虑spec
等价类划分
正负,奇偶,整数非整数,具体某格式和非该格式,长度限制,数据上限
考虑边界值
边界值及边界值附近
/*
* Testing Strategy
*
* Partition the inputs as follow
* text.length() : 0, 1, >1
* start : 0, 1, 1<start<text.length(), text.length()-1, text.length()
* text.length()-start : 0, 1, even>1, odd>1
*
* Include ... because ...
*
*/
// cover text.length() = 0
// start = text.length = 0
@Text
public void testXXX(){
...
}