Exceptional Handling in c#.

In C#, errors in the program at run time are propagated through the program by using a mechanism called exceptions. Exceptions are thrown by code that encounters an error and caught by code that can correct the error. Exceptions can be thrown by the .NET Framework common language runtime (CLR) or by code in a program. Once an exception is thrown, it propagates up the call stack until a catch statement for the exception is found. Uncaught exceptions are handled by a generic exception handler provided by the system that displays a dialog box.
Exceptions are represented by classes derived from Exception. This class identifies the type of exception and contains properties that have details about the exception. Throwing an exception involves creating an instance of an exception-derived class, optionally configuring properties of the exception, and then throwing the object by using the throwkeyword. For example:
try
{
   code which has a probability to raise an exception
}
Catch(Exception Ex)
 Handle the exception
}


            a obj = new a();
            obj.no = 12;  
            try
            {
                MessageBox.Show(Convert.ToString(obj.no));
            }
            catch(NullReferenceException EX)
            {
                MessageBox.Show("Object not found " + EX.Message);
            }
            catch (Exception EX)// Default
            {
                MessageBox.Show("Divide by zero exception found " + EX.Message);
            }

Try - Catch - Finally
Code written in finally block will be executed even when we have an exception in our code.
Code like closing database connections, file operations closing ..etc operations should be here.


Exceptional Handling in c#. Exceptional Handling in c#. Reviewed by Share less to Learn More on 6:59 PM Rating: 5

No comments