简体   繁体   English

在这种情况下,Rethrow是一个例外

[英]Rethrow an exception is this case

I have something like this: 我有这样的事情:

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
  }

}

But this: 但是这个:

throw new ApplicationException(errorMessage);

Will result in: 将导致:

An exception of type 'System.ApplicationException' occurred in ...dll but was not handled in user code 在... dll中发生了'System.ApplicationException'类型的异常,但未在用户代码中处理

How to do give the custom errror message to the caller of my above mentioned method ? 怎么做自定义errror消息给我上面提到的方法的调用者?

First, use a custom exception or at least one more meaningful instead of ApplicationException . 首先,使用自定义异常或至少一个更有意义的异常而不是ApplicationException Second, you have to catch the exception if your method throws it. 其次,如果你的方法抛出它,你必须捕获异常。

So the calling method should also wrap the method call in a try...catch : 所以调用方法也应该在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 
}

Here's an abstract, simplified example: 这是一个抽象的简化示例:

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);
    }
}

But note that you should not use exceptions for normal program flow. 但请注意,不应将异常用于正常的程序流程。 Instead you could either return true or false to indicate if the action was successful or use an out parameter for the byte[] like int.TryParse (or the other TryParse methods). 相反,您可以返回truefalse以指示操作是否成功,或者使用out byte[]out参数 ,如int.TryParse (或其他TryParse方法)。

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
    }

    }

OR 要么

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, e);
//but this does not allow the method
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM