Users Pricing

articles

Home Articles Exception Handling in C Sharp – MindStick

Exception Handling in C Sharp

Anonymous User 10219 14 Jul 2010 Updated 31 Aug 2026

Handling of error occurring at Run-time is known as Exception handling. Exception handling is a way to prevent application from crashing.
 

C# provides three keywords to handle exceptions.

·         try

·         catch

·         finally

The try block

The lines of code which we think can generate error; we put them in try block. If exception is raised code compilation is transferred to catch block.

The catch block

When exception occurs code compilation is transferred to catch block and code inside catch block is executed. We can have more than one catch block for one try block.

The finally block

Finally block is executed after try block regardless of the occurrence of error. Finally block contains all cleanup codes. For example, if there is no need of open connection to the database after try block we can write code for connection close in finally block.

Example

This code is written inside try block, as there is possibility that we could be trying to insert same record already present in database with unique attribute or connection to the database is not open.

try
   {
   System.Data.SqlClient.SqlCommandBuilder cb;
   cb = new System.Data.SqlClient.SqlCommandBuilder(da);
   DataRow dr = dt.Rows[count];
   dr[0] = txtId.Text;
   dr[1] = txtName.Text;
   dr[2] = txtAddress.Text;
   da.Update(ds, "Employee");
   MessageBox.Show("Data Updates");
    }

Catch block will catch any error occurred in try block and display message box with exception detail.

  catch (Exception ex)
    {
      MessageBox.Show(ex.Message);
    }

Finally block will be executed regardless of error occurred or not.

            finally
            {
                con.Close();
            }

There should be no code between these blocks.  

Following are some common exception classes.

Exception Class

Causes
SystemExceptionA failed run-time checks; used as a base class for other.
AccessExceptionFailure to access a type member, such as a method or field.
ArgumentExceptionAn argument to a method was invalid.
ArgumentNullExceptionA null argument was passed to a method that doesn't accept it.
ArgumentOutOfRangeExceptionArgument value is out of range.
ArithmeticExceptionArithmetic over - or underflow has occurred.
ArrayTypeMismatchExceptionAttempt to store the wrong type of object in an array.
BadImageFormatExceptionImage is in the wrong format.
CoreExceptionBase class for exceptions thrown by the runtime.
DivideByZeroException An attempt was made to divide by zero.
FormatExceptionThe format of an argument is wrong.
IndexOutOfRangeExceptionAn array index is out of bounds.
InvalidCastExpressionAn attempt was made to cast to an invalid class.
InvalidOperationExceptionA method was called at an invalid time.
MissingMemberExceptionAn invalid version of a DLL was accessed.
NotFiniteNumberExceptionA number is not valid.
NotSupportedExceptionIndicates that a class does not implement a method.
NullReferenceExceptionAttempt to use an unassigned reference.
OutOfMemoryExceptionNot enough memory to continue execution.
StackOverflowExceptionA stack has over flown.

 


I am a content writter !


1 Comments


Markdown for AI

A clean, structured version of this page for AI assistants and LLMs.

Open .md