Structured Exception Handling


Structured Exception Handling (SEH) allows you to catch specific errors as they occur and perform any appropriate action needed. In many cases, you just want to log the error and stop execution, but there are some instances where you may want to try a different plan of action, depending on the error.

Here is an example of exception handling in Visual Basic.NET.

 Public Sub Main()     Try       Dim fileText As String       FileIO.FileSystem.ReadAllText("C:\data.csv")     Catch ex As System.IO.FileNotFoundException       'Log Error Here       Dts.TaskResult = Dts.Results.Failure       Return     End Try     Dts.TaskResult = Dts.Results.Success End Sub 

This trivial example attempts to read the contents of the file at C:\data.csv into a string variable. That is why this code was placed in a Try block. It is trying to perform an action that has the potential for failure. If the file isn't there, a System.IO.FileNotFoundException is thrown. The Catch block is the error handler for this specific exception. You would probably want to add some code to log the error inside the Catch block. The result is set to Failure and the script is exited with the Return statement. If the file is found, no exception is thrown, and the next line of code is executed. In this case, it would go to the line that sets the TaskResult to Success, right after the End Try statement.

If an exception is not caught, perhaps because an appropriate Catch statement is not in place, the exception propagates up the call stack until an appropriate handler is found. If none is found, the exception stops execution.

The same code executed with the Catch statement removed will raise an error window like that in Figure 7-18.

image from book
Figure 7-18

You can have as many Catch blocks associated with a Try block as you wish. When an exception is raised, the Catch blocks are walked from top to bottom until an appropriate one is found that fits the context of the exception. Only the first block that matches will be executed. Execution does not fall through to the next block, so it's important to place the most specific Catch block first and descend to the least specific. A Catch block specified with no filter will catch all exceptions.

Another feature of SEH is the Finally block. The Finally block exists inside a Try block and executes after any code in the Try block and any Catch blocks that were entered. Code in the Finally block is always executed, regardless of what happens in the Try block and in any Catch blocks. You would put code to dispose of any resources such as open files or database connections in the Finally block. Following is an example of using the Finally block to free up a connection resource.

 Public Sub Main()     Dim con As SqlConnection = New SqlConnection(myConStr)     Try       con.Open()       'do stuff with con     Catch ex As SqlException       'Log Error Here       Dts.TaskResult = Dts.Results.Failure       Return     Finally       If Not con Is Nothing Then con.Dispose()     End Try     Dts.TaskResult = Dts.Results.Success End Sub 

Note

For a full explanation of the Try/Catch/Finally structure in Visual Basic.NET, see the language reference in MSDN.



Professional SQL Server 2005 Integration Services
Wireless Java : Developing with Java 2, Micro Edition
ISBN: 189311550X
EAN: 2147483647
Year: 2006
Pages: 182

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net