A simple Exception Handling example – C Sharp/C#

By | June 18, 2011

Hi,

As you know an exception is an error that occurs during the runtime.
Here is a simple exception handling example using C Sharp/C#.

using System;

class MainClass{

    public static void Main(){
        int value= 0;
        try {
            int i = 7/value;
        }
        catch (DivideByZeroException e) // catching divide by zero exception
        {
            Console.WriteLine("DivideByZero {0}", e);
        }
        catch (Exception e) // catching any other exceptions
        {
            Console.WriteLine("Exception {0}", e);
        }
    }
}

🙂

Leave a Reply

Your email address will not be published. Required fields are marked *