asp.net error handling mechanism--practical skills

Source: Internet
Author: User
Tags error handling exception handling finally block classic asp
The most basic requirement of program robustness is the process and capture of program error, in asp.net, the error processing has the same mechanism as other programming languages, you can use try ... Catch... Finally and so on, this is compared with the ASP has a greater progress. Moreover, using these error-handling methods can greatly improve the readability of the program and the speed of the program debugging, in the combination of these advantages, we should pay more attention to this point.
For error handling, we can refer to this article:

Try ... Catch... Finally in asp.net

Introduction
Error Handling in Classic ASP is not the best. We were have only limited options available for error handling in Classic ASP such as, ' On Error Resume Next '. In ASP 3.0 we saw the new ASP object called Error object. But we were not able to handle all exception/errors efficiently. Now in asp.net we have a new error handling mechanism which is already their in other languages such as C, C + + and JAVA. We can also call the Try...catch mechanism as "Exception handling"

What is Try ... Catch.... Finally
This is a new error handling mechanism in vb.net, so as  in ASP.NET. Well we have three blocks of code, were  Each block has it own functionality. the try ... Catch... finally block of code surrounds the code where an exception  Might occur. the simple try statement comes before the block  of code, the catch block of code is where we specify  what type of error to look for, and the finally block  of code is always executed and contains cleanup routines  For exception situations. since the catch block is specific to  the type of error we&nbsP;want to catch, we will often use multiple catch blocks in  our try ... Catch... finally structure. 

A Simple Database operation
Dim mysqlconnection as New SqlConnection (ConnectionString)
Dim Mysqlcommand as SqlCommand
Dim strSQL as String

strSQL = "INSERT into yourtable (F1, F2) VALUES (' F1 ', ' F2 ')"
Mysqlcommand = new SqlCommand (strSQL, mysqlconnection)

Try

Mysqlconnection.open ()
Mysqlcommand.executereader (commandbehavior.closeconnection)
Message.Text = "New Forward information added"

Catch Sqlexc as SqlException

Message.Text = Message.Text + sqlexc.tostring ()

Catch Exc As exception

If Instr (1, exc.tostring, "duplicate key") > 0 Then
Message.Text = Message.Text + "Cannot insert duplicate values."
Else
Message.Text = Message.Text + exc.tostring ()
End If

Finally

Mysqlconnection.close ()
End Try


What does the above example exactly do?
Well, in the above example we were trying to insert some  values to a database table. The possible chances while  performing a database operation are invalid connection string,  database server too busy resulting in connection time out,  database server not currently running etc etc. we should  Anticipate all these errors while performing a database operation.  so, we have a try block, which contains the statements  such as opening the connection and executing the operation.  Basically, we have two major statements inside the try  Block which may result in an exception/error. 

As I said, any exception can occur during a database operation. Catching all these exception are now very easy with the Catch block. All we need are to have a Catch block. We can have any number of Catch blocks. Each Catch blocks may have a different error/exception trapping mechanism. In the above example, we have two catch blocks, one which captures a general exception and the other one which traps the S Qlexception.

When all the statements inside the catch blocks are executed, the finally blocks comes into the picture. As I said earlier, finally blocks contains cleanup routines for exception.

Exit Try Statement
We can also have the Exit Try statement inside any of the try...catch blocks. The objective of this statement be to break out of the Try or Catch block. Once the Exit Try statement is executed, the control goes to the Finally block. So, Exit Try statement can is best used were we need to execute the cleanup routines.

How about nested Try statments?
We can have nested Try and Catch blocks. Can you imagine while we should use nested try statements. So, errors can occur within the Catch portion of the Try structures, and cause further exception to occur. The ability to nest try structures are available so we can use a second try structure to cover.

Links
http://www.vbweb.co.uk/show/1889/2/http://www.oreillynet.com/pub/a/dotnet/2001/09/04/error_handling.html?page=2
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.