.NET进阶之路 - API封装

1.异常抛出

封装API时,若是参数为必填项,检测参数不合格时,应当抛出传入参数不正确的异常(绝对不容许的错误,也应该抛出异常)web

public int test(string arg1, string arg2, out string msg){
    if(string.IsNullOrEmpty(arg1)) 
        throw new Exception("arg1 不能为空");
}

封装API时,若是参数没有限制,当执行过程当中,因参数问题致使程序出错,则应将参数的问题返回svg

public int test(string arg1, string arg2, out string msg){
    int result = Fun_Test(arg1);
    //result 结果表示的意思是执行失败,则根据result的类型,来指定错误内容
    if(result == -1){
        msg = "***********";
    }
    return -1
}