我有 3 种方法,其中我在大多数内部方法中进行验证。我想知道验证是否失败以及验证失败时的错误消息。我知道我可以使用异常来执行以下操作来传递错误消息..
String methodA(parms){
try{
return methodB(params);
}
catch(UserDefinedRuntimeException ure){
return ure.getMessage();
}
}
String methodB(params){
try{
return methodC(params);
}
catch(UserDefinedRuntimeException ure){
throw ure;
}
catch(Exception otherException){
//handle Exception
otherException.printStackTrace();
}
}
String methodC(params){
if(params.equals("A")||params.equals("B")||params.equals("C")){
return dosomething();
}
else{
throw UserDefinedRuntimeException("invalid input :params should be in [A,B,C]");
}
}
Run Code Online (Sandbox Code Playgroud)
但问题是很多人说创建异常很昂贵
所以我通过谷歌搜索我发现了另一种方法
像下面..
class Response{
int status;
String errMsg;
}
String methodA(params){
Response response = methodB(params);
if(response.status == 1){
return "success";
}
else{
return response.errMsg;
}
} …Run Code Online (Sandbox Code Playgroud) 像java中的array.length是否在c ++中有任何内置方法来查找数组的大小?
我知道长度().但它只适用于字符串......
我试过这个......
int a[10];
a[0]=1;
a[1]=2;
print(sizeof(a)/size(a[0]))
Run Code Online (Sandbox Code Playgroud)
但它输出为10但是有一种方法只能输出2