Exception Handling of robust page structures in ASP

Source: Internet
Author: User
Error handling is one of the things that make programmers complain. Let's face it. We just don't write the wrong code... Or a similar idea. Unfortunately, there may be many causes of runtime errors in the Code, from hardware and software changes to code used by other development teams. It is the responsibility of every conscientious programmer to effectively handle these errors and minimize the interruptions to normal website operations.

In the scope discussed in this article, there are three different places where errors can occur: scripts, middleware, and internal IT architecture. Errors in the IT internal architecture, such as the ability to degrade cyclically and lead to iis (inetinfo.exe) crashes are almost unavoidable. This type of error can only be called to request technical support and will keep the system administrator busy for a long time. Developers cannot do anything to block such errors, but we can usually handle and correct errors in scripts and middleware. After IIS is installed, the default server script language is set to VBScript. Many Web development teams maintain these default settings in their development environments, which is unfortunate because VBScript has poor support for handling runtime errors. In VBScript, the only error handling structure that developers can use is

On Error resume next (enable the error processing function) and
On Error goto 0 (Disable Error Handling)

To effectively use this error handling structure on your ASP page, you may need to use these structures to include code that may throw exceptions, as shown below:

<%
Dim myvar
On Error resume next
'The following line of code will generate an error if MSXML 4.0 is not installed or damaged.
Set myvar = server. Createobject ("msxml2.domdocument. 4.0 ")
If err. Number <> 0 then
'Handle errors here
'End error handling to prevent future errors from being detected
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 detected
On Error goto 0
End if
%>

As you can see, if you want to use the above rules on each line of existing code that may cause errors, your program will be immediately filled with "on error" and "If err. number <> 0 then... "This structure.

On the other hand, JScript provides built-in support for the robust error handling mechanism "structured exception handling (seh. Using seh allows your software development team to smoothly transfer to the. NET environment, because seh is the default error handling mechanism of JScript. net, VB. NET, and C. (Note:. Net does not support VBScript .) The following example code executes the same operations as VBScript code, but uses the JScript Language and seh to handle exceptions.

<% @ Language = "jscript" %>
<%
VaR myvar;
Try {
Myvar = server. Createobject ("msxml2.domdocument. 4.0 ");
// If an error occurs, catch
// The code block will be executed immediately
// Perform necessary operations on myvar.
}
Catch (e ){
// Handle exceptions here. Exceptions can be used
// Reference the 'E' variable.
}
Finally {
// Complete all finishing work here
// No matter whether the error occurs or not
// (Whether the "catch" block is running)
// All are executed.
}
%>

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

 

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.