C# 在这种情况下,重新显示一个异常

C# 在这种情况下,重新显示一个异常,c#,exception,C#,Exception,我有这样的想法: public byte[] AnyMethod(){ try { ... } catch (Exception e) { string errorMessage = "Some custom message, which should the caller of that method should receive"; // I thought something of this ,to pass through my cu

我有这样的想法:

public byte[] AnyMethod(){

  try {
    ...
  }
  catch (Exception e) {
    string errorMessage = 
      "Some custom message, which should the caller of that method should receive";

    // I thought something of this ,to pass through my custom exception to the caller?!
    throw new ApplicationException(errorMessage);

    //but this does not allow the method
  }

}
但这是:

throw new ApplicationException(errorMessage);
将导致:

“System.ApplicationException”类型的异常在…dll中发生,但未在用户代码中处理

如何将自定义错误消息提供给上述方法的调用方

publy byte[] AnyMethod(){

try{


}catch(Exception e){

    string errorMessage = string.Format("Some custom message, which should the caller of that method should receive. {0}", e);

    //I thought something of this ,to pass through my custom exception to the caller?!
    throw new ApplicationException(errorMessage);
    //but this does not allow the method
    }

    }


首先,使用自定义异常或至少一个更有意义的异常,而不是
ApplicationException
。其次,如果您的方法抛出异常,您必须捕获它

因此,调用方法还应该将方法调用包装在一个
try…catch

try
{
    byte[] result = AnyMethod();
}catch(MyCustomException ex)
{
    // here you can access all properties of this exception, you could also add new properties
    Console.WriteLine(ex.Message);
}
catch(Exception otherEx)
{
    // all other exceptions, do something useful like logging here
    throw;  // better than throw otherEx since it keeps the original stacktrace 
}
下面是一个抽象、简化的示例:

public class MyCustomException : Exception
{
    public MyCustomException(string msg) : base(msg)
    {
    }
}

public byte[] AnyMethod()
{
    try
    {
        return GetBytes(); // exception possible
    }
    catch (Exception e)
    {
        string errorMessage = "Some custom message, which should the caller of that method should receive";
        throw new MyCustomException(errorMessage);
    }
}

但请注意,不应将异常用于正常程序流。相反,您可以返回
true
false
以指示操作是否成功,或者对
byte[]
使用类似
int.TryParse
(或其他
TryParse
方法)的
。其次,如果您的方法抛出异常,您必须捕获它

因此,调用方法还应该将方法调用包装在一个
try…catch

try
{
    byte[] result = AnyMethod();
}catch(MyCustomException ex)
{
    // here you can access all properties of this exception, you could also add new properties
    Console.WriteLine(ex.Message);
}
catch(Exception otherEx)
{
    // all other exceptions, do something useful like logging here
    throw;  // better than throw otherEx since it keeps the original stacktrace 
}
下面是一个抽象、简化的示例:

public class MyCustomException : Exception
{
    public MyCustomException(string msg) : base(msg)
    {
    }
}

public byte[] AnyMethod()
{
    try
    {
        return GetBytes(); // exception possible
    }
    catch (Exception e)
    {
        string errorMessage = "Some custom message, which should the caller of that method should receive";
        throw new MyCustomException(errorMessage);
    }
}

但请注意,不应将异常用于正常程序流。相反,您可以返回
true
false
以指示操作是否成功,或者对
byte[]
使用类似
int.TryParse
(或其他
TryParse
方法)的
,如果您捕获到该异常,您将得到您提供的消息。顺便说一句…我希望真正的程序是不同的(你们捕获了泛型异常,你们丢弃了原始调用堆栈,你们丢弃了原始异常,你们抛出了一个无用的泛型异常类型…)这是一个Web服务,winforms,console,wcf还是什么?@Fabjan是一个类库,用于不同的用途platforms@kkkk00999然后Tim Schmelter的答案是要走的路。这是您在VS输出窗口中看到的,但不要担心,如果您发现异常,您将得到您提供的消息。顺便说一句…我希望真正的程序是不同的(你们捕获了泛型异常,你们丢弃了原始调用堆栈,你们丢弃了原始异常,你们抛出了一个无用的泛型异常类型…)这是一个Web服务,winforms,console,wcf还是什么?@Fabjan是一个类库,用于不同的用途platforms@kkkk00999那么蒂姆·施梅尔特的答案就是前进的道路。@Fabjan:Added acomment@Fabjan:添加评论