If you are developing an application, you should prepare for exceptions in your application. Exceptions are part of the applications. An exception means an error and it is run time error. In this blog I will explain how to avoid these unwanted exceptions in your applications.
Exceptions – As I already told you that exception is not a compile time error. Exception is a run time error. We can’t program for exception because exception occurs from your logic mistake and etc. But we can avoid dead screens and user can see custom error page. Take advantages of Exception handling, we can get and handle run time errors. For examples, validating user input, checking for null object, etc are all examples of great standard Exception handling. System.Exception is the base class for all the exceptions.
Category of Exception –
List of the some useful properties of Exception –
List of the some useful public methods of Exception –
As I above told you that when exceptions are thrown, you need to be able to handle them. There is a try-catch-finally blocks great feature of C Sharp. try-catch-finally blocks are able to handle exceptions. Code that could throw an exception is put in the try block an exception handling code goes in the catch block. There can be one or more catch blocks in try-catch-finally. If we want some code every time execute whether exception might occur or not, in this situation we can write our code in finally block. Below I am giving a simple example of try-catch. In below example, if user click on button without enter value in textbox, it would give an error, else it will run successfully.
<form id="form1" runat="server">
<asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
<asp:Button ID="btnTest" runat="server" onclick="btnTest_Click" />
public partial class ExceptionInASPNET : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void btnTest_Click(object sender, EventArgs e)
int x = Convert.ToInt32(txtTest.Text);
int y = 5;
int sum = x + y;
Response.Write("Sum is :" + sum);
catch (Exception ex)
Response.Write("Exception is : " + ex.ToString());
Response.Write(" Welcome.This finally block is every time excuted. " );