Introduction
One of the main features of ASP.NET over ASP is it's new error handling features. The only way to capture errors in the ASP/VBScript was using the "On Error Resume Next" statement and checking each line for an error with "If Err.Number <> 0 Then " statements. Developers who where using JScript instead of the VBScript at the server-side where lucky. They've had access to the "Try... Catch" statements provided by JScript. Well it's all over now. Now all the .NET enabled languages support better error handling statements including VB.NET. Now, VB.NET supports the "Try...Catch" statement. On top of this ASP.NET configuration file also supports ASP.NET level error handling. Let's see all of them one by one.
VB.NET Error Handling
In the previous versions of VB we had several flavors of "On Error" statements to handle error in a non-structured way. But in VB.NET we can make use of the structured way of error handling with the Try & Catch statement.
Here is an example for this.
Sub DoSomething()
Try
'Do Something
Catch e As Exception
'Catch the error and display it.
Response.Write("An Error Occurred: " & e.toString())
Finally
'Do the final cleanup such as closing the Database connection
End Try
End Sub
The above code is the simplest form of structured error handling in VB.NET. We're doing our processing in the Try Block. If anything goes wrong in the Try block, the Catch block will be called and the error will be handled. After that the "Finally" block will be called to do the cleanup.
We can also filter the error handling based on the type of exceptions. For example, Let's say we're accessing data from SQL Server 2000 and we're writing the data into a text file. In this process there could be three kids of error.
- SQL Server related Errors.
- File IO Errors.
- Other generic errors.
We can filter the errors based on their categories in the Catch block. Let's see an example for this.
Sub DoSomething()
Try
'Do Something
Catch SQLExp As SQLException
'Catch the error and display it.
Response.Write("An SQL Server Error Occurred: " & e.toString())
Catch IOExp As IOException
'Catch the error and display it.
Response.Write("An File IO Error Occurred: " & e.toString())
Catch e As Exception'Catch the error and display it.
Response.Write("An Error Occurred: " & e.toString())
Finally
'Do the final cleanup such as closing the Database connection
End Try
End Sub
As you can see we've categorized our error handling based on the errors. The "Catch SQLExp as SQLException" block will handle all the SQL Server errors and all File IO errors will be handled by "Catch IOExp as IOException" block. The SQLException and IOExpection classes are defined in the "System.Data.SQL" and "System.IO" namespaces. We can further customize the errors based on the error numbers,
Sub DoSomething()
Try
'Do Something
Catch SQLExp As SQLException
If SQLExp.Number = 1212 Then
Response.Write("SQL Server Error 1212 Occurred: " & e.toString())
Else
'Catch the error and display it.
Response.Write("An SQL Server Error Occurred: " & e.toString())
End If
Catch IOExp As IOException
'Catch the error and display it.
Response.Write("An File IO Error Occurred: " & e.toString())
Catch e As Exception
'Catch the error and display it.
Response.Write("An Error Occured: " & e.toString())
Finally
'Do the final cleanup such as closing the Database connection
End Try
End Sub
OR
Sub DoSomething()
Try
'Do Something
Catch When Err = 1212
Response.Write("SQL Server Error 1212 Occurred: " & e.toString())
Catch SQLExp As SQLException
'Catch the error and display it.
Response.Write("An SQL Server Error Occurred: " & e.toString())
Catch IOExp As IOException
'Catch the error and display it.
Response.Write("An File IO Error Occurred: " & e.toString())
Catch e As Exception
'Catch the error and display it.
Response.Write("An Error Occurred: " & e.toString())
Finally
'Do the final cleanup such as closing the Database connection
End Try
End Sub


Gowtham RajamanickamPosted Apr 18, 2015, 3:52 AM
good one
v vPosted Jul 25, 2007, 12:26 PM
Catch ex As Exception nbbbnmb mbnm bnm nb mb End Try