代码:
public Constructor(string vConnection_String)
{
try
{
mConnection_String = vConnection_String;
}
catch (Exception ex)
{
ExceptionHandler.CatchEx(ex);
}
}
我认为编写此程序的人“只是很小心”,但出于兴趣,在执行字符串赋值的行上会抛出什么异常?我能想到 System.OutOfMemoryException,但还有什么?
谢谢
最佳答案
Herb Sutter写几篇关于异常安全的好文章,并在one of them他展示了 3 种类型的异常安全:
基本保障
强有力的保障
不扔保证
这些原则在 C++ 世界中广为人知,但我们也可以在 .net 世界中使用它们,因为其中一个发生在您的情况中。
如果 mConnection_String 是一个类型为 System.String(或其他引用类型)的字段,而您肯定知道,则此代码是“无抛出保证”,因为简单赋值 引用类型 根本无法抛出异常。
关于c# - 除了 System.OutOfMemoryException 之外,string = string 还会抛出哪些异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3575973/