1.

Which class acts as a base class for all exceptions in C#?

Answer»

The base class for all exceptions is the System.EXCEPTION class. All of the exception CLASSES in C# are mainly derived from the System.Exception class whether DIRECTLY or INDIRECTLY. Some of the classes that are derived from the System.Exception class are System.ApplicationException and System.SystemException classes.

A program that demonstrates exception handling in C# is given as follows:

using System; namespace Demo {   class Program   {      static void Main(string[] args)      {          int ans = 0;          int num1 = 17;          int num2 = 0;          try         {            ans = num1 / num2;         }         catch (DivideByZeroException)         {            Console.WriteLine("An Exception Occured");         }         finally         {            Console.WriteLine("Answer: {0}", ans);         }      }   } }

The OUTPUT of the above program is given as follows:

An Exception Occured Answer: 0


Discussion

No Comment Found