public class TestThrow {
public static void main(String[] args) {
System.out.println("......");
Map<String,Object> map = new HashMap<>();
map.put("isSuccess",false);
map.put("data",null);
map.put("errorMessage",null);
try {
test1(-1);
test2("1");
map.put("isSuccess",true);
map.put("data","请求成功了");
} catch (Exception e) {
System.out.println("异常信息:"+e.getMessage());
map.put("errorMessage",e.getMessage());
}
System.out.println(map);
System.out.println("......");
}
public static void test1(int test1) {
if(test1<=0){
throw new RuntimeException("test1必须大于0");
}
System.out.println("test1="+test1);
}
public static void test2(String test2) {
if(!"test2".equals(test2)){
throw new RuntimeException("test2参数值不对");
}
System.out.println("test2="+test2);
}
}
......
异常信息:test1必须大于0
{data=null, errorMessage=test1必须大于0, isSuccess=false}
......
......
test1=1
test2=test2
{data=请求成功了, errorMessage=null, isSuccess=true}
......