The exception handling of robust page structure in ASP

Source: Internet
Author: User
Tags error handling exception handling goto iis
Within the scope of this article, there are three different places where errors can occur: scripts, middleware, and it internal architectures. Errors in the IT architecture, such as cyclical performance degradation and the resulting IIS (Inetinfo.exe) crash, are almost inevitable. This type of error usually calls for technical support and will keep the system administrator busy for a long time. Developers can't do anything to stop this kind of error, but we can usually cope with and correct the frog in the script and middleware? After installing IIS, the default server-side scripting language is set to VBScript. Many web development teams maintain these defaults in their development environment, which is unfortunate because VBScript is very poor at handling run-time errors. The only error-handling structure that developers can use in VBScript is

On Error Resume Next (open error handling function) and
On Error GoTo 0 (Turn off error handling)

To effectively use this error-handling structure in your ASP pages, you might want to use these constructs to include code that might throw exceptions, as follows:

<%
Dim MyVar
On Error Resume Next
' The following line of code generates an error if MSXML 4.0 is not installed or is corrupted
Set MyVar = Server.CreateObject ("MSXML2. domdocument.4.0 ")
If err.number <> 0 Then
' Dealing with the error here
' End error handling to prevent future errors from being discovered
On Error GoTo 0
Else
' MyVar now points to an instance of MSXML 4.0 DOMDocument
' End error handling to prevent future errors from being discovered
On Error GoTo 0
End If
%>

As you can see, if you want to use the above rules on every line of existing code that may be wrong, your program will soon be filled with "On error" and "if Err.Number <> 0 Then ..." Such a structure.

JScript, on the other hand, has built-in support for the robust error-handling mechanism, structured exception handling (SEH). Using SEH enables your software development team to move smoothly to the. NET environment, because SEH is the JScript.NET, vb.net, and C # default error handling mechanism. (Note:.) NET does not support VBScript. The following example code performs the same operations as VBScript code, but uses a JScript language and SEH to handle exceptions

<%@ language= "JScript"%>
<%
var MyVar;
try {
MyVar = Server.CreateObject ("MSXML2"). Domdocument.4.0 ");
If there is an error, then catch
The code block will be executed immediately.
and perform the necessary operations on the MyVar.
}
catch (e) {
Handling exceptions here, the exception itself can be used
The ' e ' variable is referenced.
}
finally {
To do all the finishing work here.
This code doesn't matter if it's wrong or not.
(i.e. "catch" blocks are not running)
Will execute.
}
%>

By using JScript on the server side, you get the benefits of SEH and the full use of complex ASP objects, such as server,request and response objects. To set this scripting language as the default language for your ASP page, you simply add @language instructions to your ASP page, as in the example above.



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.