Try ... Catch ... Finally in ASP.NET
page 1 of 1
Published: 17 Oct 2003
Unedited - Community Contributed
Abstract
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 mechanism which was already their in other languages such as C, C++ and JAVA. We can also call the try...catch mechanism as "Exception Handling"
by Jesudas Chinnathampi (Das)
Feedback
Average Rating: 
Views (Total / Last 10 Days): 26211/ 40

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

Written on: Mar 7th, 2002.
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 mechanism which was 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 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 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 mechanism. 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 should 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/ http://www.oreillynet.com/pub/a/dotnet/2001/09/04/error_handling.html?page=2

Send your comments to das@aspalliance.com



User Comments

Title: tgbbdghfghe   
Name: yrherhryej
Date: 2012-08-29 8:34:22 AM
Comment:
etyjyhnfghngc xvcvbhyrgbbdgdvcbcxvhsfghsdfhfhgdfh
Title: how to save the logfiles of log4net in testfile,if database connection is problem from web.config   
Name: subrahmanyam
Date: 2011-04-05 5:41:09 AM
Comment:
how to write the code.please help me urgent.
Title: need information   
Name: nene
Date: 2011-03-12 2:10:05 AM
Comment:
how to show error messages in try & catch in ASP.NET??
Title: information   
Name: me
Date: 2011-01-05 12:52:18 AM
Comment:
please give me more information. this is not enough.
Title: Worth reading   
Name: Muthu
Date: 2010-03-07 11:21:04 PM
Comment:
The article is simple and easy to understand. Thanx a lot:)
Title: Why ask for a url?   
Name: Carter Cole
Date: 2009-12-16 10:54:13 AM
Comment:
why ask for a url if you arent going to link to it? even if it has nofollow? i dont get it do yall need some seo help? im http://blog.cartercole.com
Title: exactly what i needed   
Name: Carter Cole
Date: 2009-12-16 10:52:01 AM
Comment:
try catch worked great in vb.net thanks for the article
Title: mr   
Name: T
Date: 2008-11-27 6:57:42 PM
Comment:
I think in the "Finally" section need to check that the connection open or not, because if something fail in the mySqlConnection.Open() then the mySqlConnection.Close() also throws an error, because there is no open connection.
Title: It is ok   
Name: veerathevan.m
Date: 2008-07-01 12:38:41 PM
Comment:
This is not enough,because u give some c# and asp.net coding
example it's really help me
Title: It is important   
Name: intona
Date: 2008-04-06 7:13:44 AM
Comment:
It is something useful topic. can anyone explain it by a clear example in ASP.NET
Title: Sorry   
Name: Ahmad khan
Date: 2008-04-06 7:10:38 AM
Comment:
Can you please put a litle more shade light on this topic
Title: OK but u should explain more   
Name: Riya
Date: 2008-03-03 4:08:12 AM
Comment:
Ok but not very clear.
Title: bad article   
Name: Sonali
Date: 2007-12-10 5:47:47 PM
Comment:
i study java but this article mentions nothing ABOUT try-catch! They have only mentioned random, wierd stuff!
Title: ok   
Name: suri
Date: 2007-11-27 5:59:19 AM
Comment:
exactly this.... the try block is the exception of ?
Title: Need to elaborate   
Name: Shilpa Deshmukh
Date: 2007-10-03 1:51:27 AM
Comment:
This article is ok but need to elaborate more on catching the database Exception.
Title: what about performance   
Name: nashville Vampire 13
Date: 2007-09-17 3:52:38 AM
Comment:
how does nesting try statements affect the performance of the application? is it advisable really to nest them?
Title: Nice Illustration   
Name: Shreepad Deshpande
Date: 2007-06-26 2:10:07 AM
Comment:
Nice.
Title: Good ..Thank You.   
Name: Roshna.
Date: 2007-06-25 5:10:22 PM
Comment:
I had a question about the multiple catch blocks in a try block. and you answered it in the article. It is very precise and clear. Thank you again.
Title: Not Good   
Name: Kunal
Date: 2007-03-30 8:15:10 AM
Comment:
I want some Technical explanation from ur side
But
I not got what I want......
Title: Try ... Catch ... Finally in ASP.NET   
Name: Ganeshwari
Date: 2007-03-03 3:59:17 AM
Comment:
It cleare my problem
thanks
Title: good   
Name: sharath
Date: 2007-01-31 6:54:10 AM
Comment:
This article was simple and very good..
Title: NICE   
Name: Balram Singh
Date: 2006-12-26 3:22:08 AM
Comment:
This Code article is Good. and its helping me to understanding abt try Catch Statement.

thanks a lot
Title: Ravcom   
Name: Ravindra Nath K.V.G.
Date: 2006-10-07 8:35:14 AM
Comment:
Well! ur article is good, and if it describes the disadv by using multiple catch blocks it will be fullfilled the article
Title: Hmmm   
Name: JK
Date: 2006-09-21 9:08:50 AM
Comment:
Is fine, providing you're not writing your asp.net in C# as Exit Try does not exist. :o(
Title: nice   
Name: creative
Date: 2006-09-12 12:32:25 AM
Comment:
this article is simple and nice
Title: nice one dude   
Name: newbee
Date: 2006-09-07 11:26:50 AM
Comment:
jesudas, your article is simple and concise, yet illustrates the overall idea..bookmarked :-)
thanks
Title: concise   
Name: me
Date: 2006-02-01 2:52:09 PM
Comment:
the article is short but concise.
good one.
Title: Yay   
Name: Lostinspace
Date: 2004-10-25 8:20:25 PM
Comment:
Yay it worked good and anally.
Title: sreego   
Name: praveen
Date: 2004-08-20 3:06:23 AM
Comment:
this article is gr8....it has helped me a lot...
thanx






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 7:46:08 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search