Throwing an Exception

Write short notes in throwing an exception .

Throwing an Exception

In C#, it is possible to throw an exception programmatically. The 'throw' keyword is used for this purpose. The general form of throwing an exception is as follows. 

  1. throw exception_obj; 

For example, the following statement throws an ArgumentException explicitly.

  1. throw new ArgumentException("Exception");
  2.   
  3. //C#: Exception Handling:
  4. using System;
  5. class MyClient
  6. {
  7.     public static void Main()
  8.     {
  9.         try
  10.         {
  11.             throw new DivideByZeroException("Invalid Division");
  12.         }
  13.         catch (DivideByZeroException)
  14.         {
  15.             Console.WriteLine("Exception");
  16.         }
  17.         Console.WriteLine("LAST STATEMENT");
  18.     }  
  19. }

 

Leave Comment

Important Topics

Title
Object Oriented Programming (OOP)
OOP vs POP
Class with Example
Objects
Access Modifiers
Encapsulation with example
C# Properties (GET, SET)
Method and Function
Abstraction
Polymorphism
Operator Overloading
Inheritance
Constructor and its Types
Exception Handling
Throwing an Exception