What is the purpose of throw keyword in C#?Throw is
used to throw exceptions explicitly from your code. It is used for the
following purposes:
To throw pre-defined exceptions from your code To throw user-defined exceptions from your code To re-throw an exception caught in the catch block to the calling method Here is an example demonstrating usage of throw keyword to throw user-defined exception: class sampleException:Exception { public sampleException(){ Console.WriteLine(Executing User Defined Exception..); } } class sampleClass { public static void Main() { try { throw new sampleException(); } catch(Exception ex) { Console.WriteLine(Exception Caught: + ex.ToString()); } } } Output of this code will be: Executing
User Defined Exception..
|