什么异常可以string = string throw除了System.OutOfMemoryException?

And*_*nZA 7 .net c# string exception

代码:

  public Constructor(string vConnection_String)
  {
     try
     {
        mConnection_String = vConnection_String;
     }
     catch (Exception ex)
     {
        ExceptionHandler.CatchEx(ex);
     }
  }
Run Code Online (Sandbox Code Playgroud)

我认为编程这个的人"只是小心点",但是出于兴趣,可以在这样的字符串赋值行上抛出什么异常?我可以想到System.OutOfMemoryException,但其他人呢?

谢谢

Ser*_*kov 3

Herb Sutter写了几篇关于异常安全的精彩文章,其中一篇展示了 3 种类型的异常安全:

  1. 基本保障

  2. 强有力的保证

  3. 无抛出保证

这一原则在 C++ 世界中众所周知,但我们也可以在 .net 世界中使用它们,因为其中一个原则发生在您的情况中。

如果 mConnection_String 是 System.String 类型(或其他引用类型)的字段,那么您肯定知道此代码是“无抛出保证”,因为引用类型简单赋值根本不会抛出异常。