为什么在c#Exception类中没有带1个Exception参数的构造函数
本文关键字:Exception 1个 参数 构造函数 c#Exception 为什么 | 更新日期: 2023-09-27 18:19:35
System.Exception
类有4个构造函数:
public Exception();
public Exception(string message);
protected Exception(SerializationInfo info, StreamingContext context);
public Exception(string message, Exception innerException);
为什么它没有这个构造函数?
public Exception(Exception innerException);
这是一个有帮助的地方。
我有一个静态的方法来获得一个开放的连接:
public static DbConnection GetOpenConnection(string dataProvider, string connectionString)
{
DbConnection dbConnection = GetDbProviderFactory(dataProvider).CreateConnection();
dbConnection.ConnectionString = connectionString;
dbConnection.Open();
return dbConnection;
}
如果发生任何错误,我希望此方法抛出自定义异常GetOpenConnectionException
。
所以我创建了自定义异常GetOpenConnectionException
:
public class GetOpenConnectionException : Exception
{
public GetOpenConnectionException(string message) : base(message)
{
}
public GetOpenConnectionException(string message, Exception innerException) : base(message, innerException)
{
}
}
由于Exception
没有构造函数public Exception(Exception innerException)
,所以我不能在GetOpenConnectionException
:中包含此代码
public GetOpenConnectionException(Exception innerException) : base(innerException)
{
}
所以我不得不用这种方式编码GetOpenConnection()
方法:
public static DbConnection GetOpenConnection(string dataProvider, string connectionString)
{
try
{
DbConnection dbConnection = GetDbProviderFactory(dataProvider).CreateConnection();
dbConnection.ConnectionString = connectionString;
dbConnection.Open();
return dbConnection;
}
catch (Exception e)
{
throw new GetOpenConnectionException("", e);
}
}
我想要的是以这种方式对GetOpenConnection()
方法进行编码:
public static DbConnection GetOpenConnection(string dataProvider, string connectionString)
{
try
{
DbConnection dbConnection = GetDbProviderFactory(dataProvider).CreateConnection();
dbConnection.ConnectionString = connectionString;
dbConnection.Open();
return dbConnection;
}
catch (Exception e)
{
throw new GetOpenConnectionException(e);
}
}
这有道理吗?
public GetOpenConnectionException(Exception innerException) : base("", innerException)
{
}
其他答案提供了实现您所要求的目标的好方法,但我相信您问题的核心是:"为什么最初不存在这种情况?"
当例程对其使用的资源和要求的参数的假设或先决条件不满足时,通常会引发异常。抛出的异常应该帮助调用方(或程序员)了解哪些假设或先决条件没有得到满足,以便他们可以相应地处理或修复代码。信息是沟通的关键部分。我甚至会冒险猜测,如果Exception类不需要序列化,就不会有空的构造函数!
将一个异常嵌套在另一个异常中意味着嵌套的异常是容器异常的原因。(Exception类的特定构造函数的文档有点指向这一点。
以下是一种可以提供更有意义信息的方法:
public static DbConnection GetOpenConnection(string dataProvider, string connectionString)
{
try
{
DbConnection dbConnection = GetDbProviderFactory(dataProvider).CreateConnection();
dbConnection.ConnectionString = connectionString;
dbConnection.Open();
return dbConnection;
}
catch (Exception e) // consider changing this to a more specific type of exception, as an error with .Open() is not the only way it could fail
{
// provide a meaningful message to the caller about the context of this error
var message = string.Format("An error occurred while trying to connect to the data provider {0} with the connection string {1}.", dataProvder, connectionString);
throw new GetOpenConnectionException(message, e);
}
}
类似于Piotr Dory的答案,您可以这样做:
public GetOpenConnectionException(Exception innerException) :
base(innerException.message, innerException)
{
}
通过这种方式,您实际上是在向基本构造函数传递一些有意义的东西。
至于为什么没有一个构造函数只接受Exception参数。。。似乎并没有什么好的理由。他们很容易就能做到。