Sample Exceptions in C#

Error Types

  • Syntax Error: Compile type error is syntax error.
  • Exception error: Run time error is exception error.
  • Logical error: If you make a mistake in a program such as use incorrect logic then show an error then it is called a logical error. “A logical error occurs when an application compiles and runs properly but does not produce the expected result.”

Exception

  • “An exception is termed as an abnormal condition by an application during its exception.”
  • An Exception is an error that occurs during program execution; exceptional situations arise when an operation can't be completed normally.
  • When an exception occurs in and application the system throws an error.

All exceptions are predefined exceptions.

Categorized of exception

  • System.Exception
  • Pre-Defined for use:
    System.SystemException
  • User Defined for Use:
    System.SystemApplicationException
  • An Exception handling is the process of providing an alternative path of execution of program, when the application is unable to execute as desired.

Example

An System.IO.IOException exception is thrown when you try to access an illegal stream object.

  • If the denominator is zero then an integer division operation throws the “System. DivideByZeroException” exception.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.   
  5. namespace Exception2  
  6. {  
  7.     class Test  
  8.     {  
  9.         static int Zero = 0;  
  10.         static void AFunction()  
  11.         {  
  12.             try  
  13.             {  
  14.                 int j = 22 / Zero;  
  15.             }  
  16.             // this exception doesn't match  
  17.             catch (DivideByZeroException e)  
  18.             {  
  19.                 Console.WriteLine(e.Message);  
  20.             }  
  21.             Console.WriteLine("In AFunction()");  
  22.         }  
  23.         //class Program  
  24.         //{  
  25.         public static void Main()  
  26.         {  
  27.             try  
  28.             {  
  29.                 AFunction();  
  30.             }  
  31.             // this exception doesn't match  
  32.             catch (ArgumentException e)  
  33.             {  
  34.                 Console.WriteLine("ArgumentException {0}", e);  
  35.             }  
  36.             Console.ReadKey();  
  37.         }  
  38.         //}  
  39.     }  

See the following output.



So friends, these are a few comments that will help you with samples of exceptions in C#. The next time I will put some more interesting comments, thank you. I hope this is helpful for you. Enjoy :).


Similar Articles