Reposted: ASP's exception handling

xiaoxiao2021-03-06  192

Error handling is one of the things that make programmers complain. Let us face it, the code we don't write is wrong. . . Or similar ideas. Unfortunately, there may be many reasons for runtime errors in your code, from hardware, software changes to code using other development teams, and so on. Effective processing of these errors and minimizes the interrupt of the normal operating process of the website is the responsibility of each conscience.

Within the scope discussed herein, there are three different places to occur: scripts, middleware, and IT internal architectures. IT internal architecture errors, reducing periodic performance, and causes IIS to crash almost unavoidable. This type of error usually can only call technical support and will allow system administrators for a long time. Developers cannot do what to prevent such errors, but we usually cope with and correct the errors in scripts and middleware. After installation of IIS, the default server-side scripting language is set to VBScript. Many web development teams have kept these default settings in their development environment, because VBScript is very poor for handling runtime errors. In VBScript, the only error handling structure that developers can use is

On Error ResMe next (Open Error Processing) and On Error Goto 0 (Turn error handling function)

In order to use this error handling structure in your ASP page, you may need to use these structures to include the code that may throw an exception, just like this: <% DIM MyVAR on Error Resume Next 'below a line of code will be in MSXML 4.0 Error is not installed or corrupt to generate an error set myvar = server.createObject ("msxml2.domdocument.4.0") if err.Number <> 0 THEN 'here handles error' End error handling, avoiding errors afterwards Discovered on Error Goto 0 else 'MyVar now pointing to an instance of MSXML 4.0 DomDocument' End Error handling, avoiding errors that have occurred in the future cannot be discovered to discover the ON Error Goto 0 end if%> Just like you see, if you want Every line of existing misunderstandings can be used on the code on the code, your program will be full of "on err.number <> 0 dam...".

On the other hand, JScript has built-in support for a robust error handling mechanism "Structured Exception Handling (SEH)". Use SEH to make your software development team to transfer to the .NET environment, because SEH is JScript.net, VB.NET, and C # default error handling mechanism. (Note: .NET does not support VBScript.) The following example code performs the same operation as the VBScript code, but uses the JScript language and use SEH to handle exceptions.

<% @ Language = "jscript"%> <% var myvar; try {myvar = server.createObject ("msxml2.domdocument.4.0"); // If there is an error above, then catch // code block will be immediately Perform / / and make the necessary operation on MyVar. } Catch (e) {// Treatment herein, exception itself can be referenced with // 'e' variables. } Finally {// Here, all end of the finished work // This code does not occur if there is an error, (i.e., "Catch" block is running) // will execute. }%> By using JScript on the server side, you get the benefits of SEH, as well as complete use of complex ASP objects, computers Server, Request, and Response objects. To set this scripting language into your default language of your ASP page, you only need to add the @language command on your ASP page, just like the example above.

转载请注明原文地址:https://www.9cbs.com/read-128047.html

New Post(0)