C# 使用新的自定义预期重写引发的异常

C# 使用新的自定义预期重写引发的异常,c#,rest,exception,exception-handling,C#,Rest,Exception,Exception Handling,我正在尝试将服务调用引发的异常覆盖到我自己的自定义异常。我想使用两个自定义异常,具体取决于服务调用返回的状态码。500+错误的ServerException或400+错误的ClientException catch (ApiException e) { if ((int)e.StatusCode >= 400 && (int)e.StatusCode < 500) { throw new ClientException(e.Status

我正在尝试将服务调用引发的异常覆盖到我自己的自定义异常。我想使用两个自定义异常,具体取决于服务调用返回的状态码。500+错误的ServerException或400+错误的ClientException

catch (ApiException e)
{
    if ((int)e.StatusCode >= 400 && (int)e.StatusCode < 500)
    {
        throw new ClientException(e.StatusCode.ToString(), e.Message, e.Uri.ToString(), e.HttpMethod, e.Content, e.StatusCode, e.InnerException.Message);
    }
    else
    {
        throw new ServerException(e.StatusCode.ToString(), e.Message, e.Uri.ToString(), e.HttpMethod, e.Content, e.StatusCode, e.InnerException.Message);
    }
}
ClientException类,因可读性而缩短:

public class ClientException : Exception
{
    public string Code { get; private set; }
    public string ReasonPhrase { get; private set; }
    public ClientException(string code, string message) : base(message)
    {
        Code = code;
        ReasonPhrase = message;
    }
 }

没有定义接受参数的方法

throw new ClientException(
    e.StatusCode.ToString(),
    e.Message, 
    e.Uri.ToString(), 
    e.HttpMethod, 
    e.Content, 
    e.StatusCode, 
    e.InnerException.Message);
类中需要一个方法,例如

public ClientException(
    string StatusCode,
    string Message, 
    string Uri 
    string HttpMethod, 
    string Content, 
    string StatusCode, 
    string InnerExceptionMessage) : base(...)
{
    ...
}
或者,您可以使用

public ClientException(params object[] args)
        : base(...) { }

我认为你的代码抛出了一个类似的异常

public void  foo(){
....
throe new ApiException();
}
在你的代码中你有

try
{
    foo();//throe ApiException
}
catch(ClientException e)
 {}
catch(ServerException e)
 {}
catch (ApiException e)
 {
     var ex=ExceptionLocator.Get(ExceptionCode);//use ExceptionLocator  to locate your custom exception  
     throw ex;
 }
catch (Exception e)
 {
  //Always caught here only
 }

public abstract class BusinessException : Exception
{
    public string Code { get; private set; }
    public string ReasonPhrase { get; private set; }
    public ClientException(string code, string message) : base(message)
    {
        Code = code;
        ReasonPhrase = message;
    }
 }

public class ClientException:BusinessException
{

}

public class ServerException:BusinessException
{

}

public static class ExceptionLocator{
    static Dictionary<int,BusinessException> locator=new Dictionary<int,BusinessException>();

    public ExceptionLocator()
    {
        locator.Add(400,new ClientException());
        locator.Add(500,new ServerException());
        ....
    }
    public static BusinessException Get(int code)
    {
        return locator[code];
    }
}

您可以使用ExceptionLocator来定位自定义异常,或者在Foo中确定抛出ClientException或ServerException

封闭方法不是异步的吗?i、 e.Task basedUsing refit for service call,因此服务调用本身是异步的,但不是保存调用的方法。您介意显示ClientException类吗?我假设它是从异常派生的,可能是一个方法实现问题。这里返回的异常的确切类型是什么?只是系统异常还是其他类型?您是否检查过代码的任何中间层,它们是否有自己的try/catch块可能是罪魁祸首?您确定抛出和捕获的异常类型相同吗?我是说他们属于同一个图书馆?您共享的异常定义没有接受两个以上参数的构造函数。您将哪个库用于ExceptionLocator?thanks@cfl我更新我的答案
try
{
    foo();//throe ApiException
}
catch(ClientException e)
 {}
catch(ServerException e)
 {}
catch (ApiException e)
 {
     var ex=ExceptionLocator.Get(ExceptionCode);//use ExceptionLocator  to locate your custom exception  
     throw ex;
 }
catch (Exception e)
 {
  //Always caught here only
 }

public abstract class BusinessException : Exception
{
    public string Code { get; private set; }
    public string ReasonPhrase { get; private set; }
    public ClientException(string code, string message) : base(message)
    {
        Code = code;
        ReasonPhrase = message;
    }
 }

public class ClientException:BusinessException
{

}

public class ServerException:BusinessException
{

}

public static class ExceptionLocator{
    static Dictionary<int,BusinessException> locator=new Dictionary<int,BusinessException>();

    public ExceptionLocator()
    {
        locator.Add(400,new ClientException());
        locator.Add(500,new ServerException());
        ....
    }
    public static BusinessException Get(int code)
    {
        return locator[code];
    }
}