通过给定参数的方法抛出异常 C#

发布于 2024-10-16 06:50:54 字数 386 浏览 2 评论 0原文

我想创建一个方法,通过我给该方法的参数抛出特定的异常。我有 3 个用户定义的异常,所以我不想每次使用它们时都抛出它们,而是想创建一个处理它的方法,所以我用我的方法给出的参数就是我想要抛出的异常,但是我该怎么做这样做吗?

我想做这样的事情,但我不太确定该怎么做。

private void ExceptionMethod(custom exception)
{
    try
    {
       //code that might fail
    }
    catch(exception ex)
    {
      throw new exception given by parameter(parameters from the exception);
    }

}

I want to make a method, that throws a specific exception, by a parameter I give to the method. I have 3 userdefined exceptions, so instead of having to throw them every time I want to use them I want to make a method that handels it, so the parameter I give with my method is the exception I want to throw, but how do I do that?

I want to do something like this, but I am not really sure how to do it.

private void ExceptionMethod(custom exception)
{
    try
    {
       //code that might fail
    }
    catch(exception ex)
    {
      throw new exception given by parameter(parameters from the exception);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

素染倾城色 2024-10-23 06:50:54

FWIW 我认为这不是一个特别好的主意。实际上,只要在发生异常的地方抛出异常,代码的未来维护者就会感谢你。 (或者至少不会诅咒你)

如果你必须做这件事,那么最好传递一个可以打开的枚举而不是异常本身,然后简单地编写一个 case 语句来抛出你想要的异常。

FWIW I don't think this is a particulary good idea. Really, just throw your exception where it occurs, future maintainers of the code will thank you. (or at least not curse you)

If you have to do this thing, then its probably a better idea to pass an enumeration that you can switch on rather than the exception itself, then simply write a case statement to throw the exception you want.

無心 2024-10-23 06:50:54

除了这听起来像是一个坏主意之外,您还可以尝试以下操作:

private void TryElseThrow<TCustomException>(Action codeThatMightFail)
    where TCustomException : Exception
{
    try
    {
        codeThatMightFail();
    }
    catch (Exception e)
    {
        // Since there isn't a generic type constraint for a constructor
        // that expects a specific parameter, we'll have to risk it :-)
        throw
          (TCustomException)Activator
            .CreateInstance(typeof(TCustomException), e);
    }
}

像这样使用:

TryElseThrow<MyCustomException>(
    () =>
    {
        throw new InvalidOperationException();
    }
);

Apart from the fact that this sounds like a bad idea, you can try the following:

private void TryElseThrow<TCustomException>(Action codeThatMightFail)
    where TCustomException : Exception
{
    try
    {
        codeThatMightFail();
    }
    catch (Exception e)
    {
        // Since there isn't a generic type constraint for a constructor
        // that expects a specific parameter, we'll have to risk it :-)
        throw
          (TCustomException)Activator
            .CreateInstance(typeof(TCustomException), e);
    }
}

Use like so:

TryElseThrow<MyCustomException>(
    () =>
    {
        throw new InvalidOperationException();
    }
);
月亮邮递员 2024-10-23 06:50:54

您实际上非常接近:

private void ExceptionMethod(Exception customException)
{
    try
    {
       //code that might fail
    }
    catch(Exception ex)
    {
        throw customException;
    }
}

会起作用,但我不会推荐它,原因有两个:

  1. 捕获Exception是一个坏主意 - 您应该只捕获代码引发的异常。
  2. 这不是一个很好的设计(正如其他人指出的那样)。

You were actually quite close:

private void ExceptionMethod(Exception customException)
{
    try
    {
       //code that might fail
    }
    catch(Exception ex)
    {
        throw customException;
    }
}

Will work, though I wouldn't recommend it for two reasons:

  1. Catching Exception is a bad idea - you should just catch the exceptions that your code raises.
  2. It's not a very good design (as others have pointed out).
纵性 2024-10-23 06:50:54

所以我没有看到任何问题......正如你所说,你已经编写了自定义异常,那么你可以这样做。

在你的参数中:

private void ExceptionMethod(CustomException myexception)

在 catch 中:

throw myexception;

虽然它不是一个好的编码设计

So i dont see any problem in that.. as you say you already have your Custom Exception Written then you can do it like this.

in your parameter:

private void ExceptionMethod(CustomException myexception)

in catch:

throw myexception;

though its not a good Coding Design

徒留西风 2024-10-23 06:50:54

难道不就是:

private void ExceptionMethod(MyCustomException exception)
{
    try
    {
       //code that might fail
    }
    catch
    {
      throw exception;
    }

}

Wouldn't it just be:

private void ExceptionMethod(MyCustomException exception)
{
    try
    {
       //code that might fail
    }
    catch
    {
      throw exception;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文