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 |
SystemException | A failed run-time checks; used as a base class for other. |
AccessException | Failure to access a type member, such as a method or field. |
ArgumentException | An argument to a method was invalid. |
ArgumentNullException | A null argument was passed to a method that doesn't accept it. |
ArgumentOutOfRangeException | Argument value is out of range. |
ArithmeticException | Arithmetic over - or underflow has occurred. |
ArrayTypeMismatchException | Attempt to store the wrong type of object in an array. |
BadImageFormatException | Image is in the wrong format. |
CoreException | Base class for exceptions thrown by the runtime. |
DivideByZeroException | An attempt was made to divide by zero. |
FormatException | The format of an argument is wrong. |
IndexOutOfRangeException | An array index is out of bounds. |
InvalidCastExpression | An attempt was made to cast to an invalid class. |
InvalidOperationException | A method was called at an invalid time. |
MissingMemberException | An invalid version of a DLL was accessed. |
NotFiniteNumberException | A number is not valid. |
NotSupportedException | Indicates that a class does not implement a method. |
NullReferenceException | Attempt to use an unassigned reference. |
OutOfMemoryException | Not enough memory to continue execution. |
StackOverflowException | A stack has over flown. |
Anonymous User
09-Mar-2019Thank You for sharing.