Simple Exceptions - .Net C#

Started by sukishan, Jul 11, 2009, 03:02 PM

Previous topic - Next topic

sukishan

Simple Exceptions - .Net C#:

   C# language uses many types of exceptions, which are defined in special classes. All of them are inherited from base class named System.Exception. There are classes that process many kinds of exceptions: out of memory exception, stack overflow exception, null reference exception, index out of range exception, invalid cast exception, arithmetic exception etc. This c# tutorial deals with DivideByZero c# exception and custom classes in c# exceptions.

   C# has defined some keywords for processing exceptions. The most important are try, catch and finally.

   The first one to be known is the try operator. This is used in a part of code, where there exists a possibility of exception to be thrown. But operator ?try? is always used with the operators: catch and finally.

  See the following example of handling a simple exception in c#.


--------------------------------------------------------------------------------

//Sample code for C# Exception tutorial using try , catch

// try catch exception
int zero = 0;
try
{
    int div = 100/zero;
}
catch(DivideByZeroException)
{
    Console.WriteLine("Division by zero exception passed");
}


--------------------------------------------------------------------------------

   This code in runtime throws a DivideByZeroException and writes some message through the console. But if you want to release some resources that were created you must use try ? finally construction. Finally will be called even if there were no exceptions raised.
A good beginning makes a good ending

dhoni

with that simple exception can easily solve program
this shows main program to have nformation