ExtensionMethod.NET Home of 818 C#, Visual Basic, F# and Javascript extension methods

ToException

String Typed Exception Extension

Source

public static void ToException<T>(this string message) where T : Exception, new()
{
      var e = (T)Activator.CreateInstance(typeof(T), message);
      throw e;
}

Example

"ferhat".ToException<FileNotFoundException>();
"ferhat".ToException<Exception>();
"ferhat".ToException<IOException>();

Author: Ferhat Celik

Submitted on: 9 jun 2015

Language: C#

Type: Generic Exception

Views: 2779