简体   繁体   English

给定参数C#引发方法异常

[英]Throw exception with method by given parameter C#

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? 我有3个用户定义的异常,所以我不必每次使用它们时都必须抛出它们,而是想创建一个处理它的方法,所以我给方法提供的参数是我想抛出的异常,但是我该怎么办去做?

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

}

FWIW I don't think this is a particulary good idea. FWIW我认为这不是一个特别好的主意。 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. 如果必须执行此操作,那么传递一个可以打开的枚举而不是异常本身可能是一个更好的主意,然后只需编写一个case语句以抛出所需的异常即可。

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

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. 捕获Exception不是一个好主意-您应该捕获代码引发的异常。
  2. It's not a very good design (as others have pointed out). 这不是一个很好的设计(正如其他人指出的那样)。

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 虽然它不是一个好的编码设计

Wouldn't it just be: 不会只是:

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

}

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

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