在开发中得到的小trick:
在开发时候会遇到抛出异常的需求,抛出的异常信息中要包含变量的名字,而不是变量的内容。例如:
String names = "sty,ldq,cjl";
String nations ="China,Japan,England";
List<String> nameList = decode(names);
List<String> nationList = decode(nations);
public static List<String> decode(String str){
//TODO 対str进行解码
// 解码失败,抛出异常。
throw new RuntimeException("Failed to decode: "+str)
}
// 这种抛出异常会只会输出Failed to decode:sty,ldq,cjl
//Failed to decode:China,Japan,England
// 而我想要的是Failed to decode:names
//Failed to decode:nations
解决办法,多加一个参数,在调用的时候把参数的名字字符串传进去:
public static List<String> decode(String str,String paraName){
//TODO 対str进行解码
// 解码失败,抛出异常。
throw new RuntimeException("Failed to decode: "+paraName)
}
List<String> nameList = decode(names,"names");
List<String> nationList = decode(nations,"nations");