Structured Exception Handling
Developing enterprise applications requires the construction of reusable, maintainable
components. One challenging aspect of the Basic language in past versions of
Visual Basic was its support for error handling. In the past, developers had
to provide error-handling code in every function and subroutine. Developers
have found that a consistent error-handling scheme means a great deal of duplicated
code. Error handling using the existing On Error GoTo statement sometimes
slows the development and maintenance of large applications. Its very name reflects
some of these problems: As the GoTo implies, when an error occurs, control
is transferred to a labeled location inside the subroutine. Once the error code
runs it must often be diverted via another cleanup location via a standard GoTo,
which finally uses yet another GoTo or an Exit out of the procedure.
Handling several different errors with various combinations of Resume
and Next quickly produces illegible code and it leads to frequent bugs
when execution paths aren't completely thought out.
With Try...Catch...Finally, these problems go away, developers can nest
their exception handling, and there is a control structure for writing cleanup
code that executes in both normal and exception conditions.
Sub SEH()
Try
Open "TESTFILE" For Output As #1
Write #1, CustomerInformation
Catch
Kill "TESTFILE"
Finally
Close #1
End try
End Sub