Sunday 2 October 2011

Whats the use of "throw" keyword in C#?


                                   The throw keyword is used to throw an exception programatically in C#. In .NET, there is an in-built technique to manage & throw exceptions. In C#, there are 3 keyword, that are used to implement the Exception Handling. These are the try, catch and finally keywords. In case an exception has to be implicitly thrown, then the throw keyword is used. See code example below, for throwing an exception programatically...
C# Example
class SomeClass
{
public static void Main()
{
 try
 {
   throw new DivideByZeroException("Invalid Division Occured");
 }
 catch(DivideByZeroException e)
 {
   Console.WriteLine("Exception - Divide by Zero" );
 } 
  }
  }

No comments:

Post a Comment