如何将一种异常作为参数传递?

时间:2021-05-17 16:37:50

I would like to have a method that receives a type of exception (i.e., the parameter passed must be a class that implements System.Exception).

我想有一个接收一种异常的方法(即传递的参数必须是一个实现System.Exception的类)。

What is the right way to do this?

这样做的正确方法是什么?

The following is NOT what I want:

以下不是我想要的:

public void SetException(Exception e)

... that requires an instance of an exception. I want to pass a type of exception like InvalidProgramException

......需要一个例外实例。我想传递一种类型的异常,如InvalidProgramException


Edit: To further explain what I want to do, I want to be able to track how many times I have seen an exception of each type before. So I want to be able to do something like:

编辑:为了进一步解释我想要做什么,我希望能够跟踪我之前看过每种类型的异常的次数。所以我希望能够做到这样的事情:

Dictionary<ExceptionTypeThing, int> ExceptionCounts;

public void ExceptionSeen(ExceptionTypeThing e)
{
    // Assume initialization
    ExceptionCounts[e]++;
}

ExceptionSeen(InvalidProgramException);

I don't want to pass an instance of the exception, but rather track it by type of exception.

我不想传递异常的实例,而是按异常类型跟踪它。

5 个解决方案

#1


Sounds like you need a generic method

听起来你需要一个通用的方法

public void SetException<TException>() where TException : Exception
{
    ExceptionCounts[typeof(TException)]++;
}

You can call it as

你可以称之为

SetException<InvalidProgramException>();

Edit:

Dictionary<Type, int> ExceptionCounts;
public void ExceptionSeen(Type type)
{
    ExceptionCounts[type]++;
}

Call it as

称之为

ExceptionSeen(typeof(MyException));

Or if you have the exception instance already

或者如果您已经拥有异常实例

ExceptionSeen(ex.GetType());

#2


If you just want to pass a type, specify 'Type' as the paramteer type, then if you want to make sure that is is of exception type you need to check the type at runtime:

如果您只想传递一个类型,请指定'Type'作为参数类型,然后如果您想确保它是异常类型,您需要在运行时检查类型:

public void SetException(Type t) {
    if (!typeof(Exception).IsAssignableFrom(t)) {
       throw new ArgumentException("t");
    }
}

#3


Define a generic method that does not take an instance, then use the generic type constraint to force it to inherit from Exception:

定义不接受实例的泛型方法,然后使用泛型类型约束强制它从Exception继承:

public void SetException<T>() where T : Exception
{
}

#4


Like this:

public void SetException(Type type)

SetException(typeof(InvalidProgramException))
// or
SetException(e.GetType)

Another options is to use generics to help

另一种选择是使用泛型来帮助

public void SetException<T>()    
SetException<InvalidProgramException>()

or

public void SetException<T>(T exception)    
SetException(e);

#5


you can create your custom class that inherit base exception i.e. exception class and then you can pass any type of exception in your method by parameter type of your custom class that you already created.

您可以创建继承基本异常(即异常类)的自定义类,然后您可以通过已创建的自定义类的参数类型在方法中传递任何类型的异常。

#1


Sounds like you need a generic method

听起来你需要一个通用的方法

public void SetException<TException>() where TException : Exception
{
    ExceptionCounts[typeof(TException)]++;
}

You can call it as

你可以称之为

SetException<InvalidProgramException>();

Edit:

Dictionary<Type, int> ExceptionCounts;
public void ExceptionSeen(Type type)
{
    ExceptionCounts[type]++;
}

Call it as

称之为

ExceptionSeen(typeof(MyException));

Or if you have the exception instance already

或者如果您已经拥有异常实例

ExceptionSeen(ex.GetType());

#2


If you just want to pass a type, specify 'Type' as the paramteer type, then if you want to make sure that is is of exception type you need to check the type at runtime:

如果您只想传递一个类型,请指定'Type'作为参数类型,然后如果您想确保它是异常类型,您需要在运行时检查类型:

public void SetException(Type t) {
    if (!typeof(Exception).IsAssignableFrom(t)) {
       throw new ArgumentException("t");
    }
}

#3


Define a generic method that does not take an instance, then use the generic type constraint to force it to inherit from Exception:

定义不接受实例的泛型方法,然后使用泛型类型约束强制它从Exception继承:

public void SetException<T>() where T : Exception
{
}

#4


Like this:

public void SetException(Type type)

SetException(typeof(InvalidProgramException))
// or
SetException(e.GetType)

Another options is to use generics to help

另一种选择是使用泛型来帮助

public void SetException<T>()    
SetException<InvalidProgramException>()

or

public void SetException<T>(T exception)    
SetException(e);

#5


you can create your custom class that inherit base exception i.e. exception class and then you can pass any type of exception in your method by parameter type of your custom class that you already created.

您可以创建继承基本异常(即异常类)的自定义类,然后您可以通过已创建的自定义类的参数类型在方法中传递任何类型的异常。