Explanation of asp.net's error handling mechanism

Source: Internet
Author: User
Tags classic asp

The most basic requirement for program robustness is the processing and capturing of program errors. in ASP. NET, the error processing mechanism is the same as that in other programming languages. You can use Try... Catch... Finally and so on, which is greatly improved compared with ASP. In addition, using these error handling methods can greatly improve the readability and debugging speed of the program. When combining these advantages, we should pay more attention to this.
For error handling, refer to this article:

Try... Catch... Finally in ASP. NET

Introduction
Error handling in Classic ASP was not the best. we were having 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 mechanic which was already their in other versions ages such as C, C ++ and JAVA. we can also call the try... catch mechanisms as "Exception Handling"

What is Try... Catch... Finally
This is a new error handling mechanic 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, 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 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 executing Ming a database operation are invalid connection string, database server too busy resulting in connection time out, database server not currently running etc. we shoshould anticipate all these errors while playing Ming 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 is now very easy with the Catch block. all we need is to have a Catch block. we can have any number of Catch blocks. each Catch block may have a different error/exception trapping mechanic. in the above example, we have two catch blocks, one which captures a general exception and the other one which traps the SqlException.

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

Exit Try statement
We can also have the Exit Try statement inside any of the try... catch block. the objective of this statement is 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 be 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, when we shoshould use nested try statements. well, errors can occur within the Catch portion of the Try structures, and cause further exception to occur. the ability to nest try structures is available so that we can use a second Try structure to cover exceptions.

Links
Http://www.vbweb.co.uk/show/1889/2? 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.