C# Error Handling Using Try-Catch-Finally

Error Handling in Asp.NET

As a programmer, no matter how much precautions we take against errors that may occur on our web pages, there will always be errors caused by visitors or other reasons. For example, if the visitor should enter information in a text box, leaving it blank or entering different types of or incorrect information may cause an error.

In such errors, we do not want our pages to stop working and the visitor to be faced with an Asp.Net error page. As a precaution, the Try - Catch - Finally structure is very useful.

By using Try - Catch, we can ensure that the actions we specify are performed in case an error occurs during the execution of the program. In this way, the user will not see confused and unwanted error pages. In addition, we can obtain the type of error that occurs and provide different actions according to the error type.

Using Try Catch Finally

try
{

   // Here we write the codes that we want to be done and that are likely to cause an error.
}
catch
{

   // If an error occurs while performing the operations written in the try block, the operations here are performed. If there is no error, this block is skipped.
}
finally
{

   // Whether there is an error or not, we write the actions that we want to be done here.
 
   // Examples include closing database connections, deleting created objects, cleaning form elements.
 
   // The finally block is optional, if not needed, it can be omitted.

}

Example: In the example below, operations are attempted with numbers entered in text boxes. If an error occurs due to user inputs or other reasons, the Catch block will be processed. Form elements will be cleared as specified in the finally block whether or not an error occurs.

try
{
    int a = int.Parse(textBox1.Text);
    int b = int.Parse(textBox2.Text);
    int c = a * b;
    label1.Text = c.ToString();
}
catch
{
    label1.Text = "An error occured!";
}
finally
    textBox1.Text = "";
    textBox2.Text = "";
}

Dealing by Error Type

With the Try Catch structure, it is also possible to get information about the cause of the error and to perform different actions accordingly.

Sample:

try
{
    int a = int.Parse(textBox1.Text);
    int b = int.Parse(textBox2.Text);
    int c = a * b;
    label1.Text = c.ToString();
}
catch (ArgumentNullException hata1)
{
    label1.Text = "Do not leave the boxes empty";
}
catch(FormatException hata2)
{
    label1.Text = "You entered the wrong type of data.";
}
catch (OverflowException hata3)
{
    label1.Text = "The value you entered is too large.";
}
finally
    textBox1.Text = "";
    textBox2.Text = "";
}

More than one catch block is used in the above example. Depending on the type of error that occurs, the relevant catch block will be executed.

There are many types of errors that can be used in catch brackets. Other error types than those in this example can also be handled.

If the resulting error matches more than one type, the upper catch block will work.

Try Catch Tutorial

We used try catch structure for FileUpload object usage.

asp.net c# error handling, using try catch and examples, what does finally do, when to use finally, how to use try catch

EXERCISES

There are no examples related to this subject.



COMMENTS




Read 784 times.

Online Users: 713



error-handling-using-try-catch