代码之家  ›  专栏  ›  技术社区  ›  Sander Versluys

在ASP Classic中是否可以进行类似try-catch的错误处理?

  •  37
  • Sander Versluys  · 技术社区  · 17 年前

    ASP Classic 用于错误处理?

    例如:

    我在用电话 Mail.SendMail

    有什么想法吗?

    8 回复  |  直到 14 年前
        1
  •  52
  •   Wolfwyrd    7 年前

    有两种方法,您可以在JScript或VBScript中编写代码,这两种方法都有构造,或者您可以在代码中伪造它。

    使用JScript可以使用以下类型的构造:

    <script language="jscript" runat="server">
    try  {
        tryStatements
    }
    catch(exception) {
        catchStatements
    }
    finally {
        finallyStatements
    }
    </script>
    

    在您的ASP代码中,您可以在下一次尝试时使用on error resume,然后在捕获点检查err.Number,如:

    <%
    ' Turn off error Handling
    On Error Resume Next
    
    
    'Code here that you want to catch errors from
    
    ' Error Handler
    If Err.Number <> 0 Then
       ' Error Occurred - Trap it
       On Error Goto 0 ' Turn error handling back on for errors in your handling block
       ' Code to cope with the error here
    End If
    On Error Goto 0 ' Reset error handling.
    
    %>
    
        2
  •  14
  •   Phil Edwards    13 年前

    如果您使用On Error Resume Next,则需要注意在它之后包含多少代码:请记住,“If Err.Number<>0 Then”这句话仅指以前触发的错误。

    否则,“出错后继续下一步”的意思就是它所说的——您的代码可以在任意多行上失败,执行将愉快地继续下去。这就是为什么这是一个痛苦的屁股。

        3
  •  8
  •   Svante Svenson    16 年前

    对于缺少的COM类,有一种很好的处理方法:

    Dim o:Set o = Nothing
    On Error Resume Next
    Set o = CreateObject("foo.bar")
    On Error Goto 0
    If o Is Nothing Then
      Response.Write "Oups, foo.bar isn't installed on this server!"
    Else
      Response.Write "Foo bar found, yay."
    End If
    
        4
  •  8
  •   James Skemp    9 年前

    1) 加 On Error Resume Next

    2) 在页面底部添加以下代码

    If Err.Number <> 0 Then
    
      Response.Write (Err.Description)   
    
      Response.End 
    
    End If
    
    On Error GoTo 0
    
        5
  •  5
  •   Levi    12 年前

    声明 出错时继续下一步

      On Error Resume Next
      'Your code logic is here
    

    然后以如下语句结束:

      If Err.Number <> 0 then
    
      'Your error message goes here'
    
      End if
    
        6
  •  3
  •   tisaconundrum    6 年前

    对于任何一个在ASP和更现代的语言领域工作过的人来说,这个问题都会引起一片笑声。根据我的经验,使用自定义错误处理程序(在IIS中设置以处理500;100个错误)是ASP错误处理的最佳选择。本文描述了这种方法,甚至给出了一些示例代码/数据库表定义。

    http://www.15seconds.com/issue/020821.htm

    link 存档.org的版本

        7
  •  2
  •   Tomalak    12 年前

    我在ASP土地已经有一段时间了,但iirc有两种方法:

    try catch finally 可以在VBS中合理模拟(好文章 here here )有一个活动叫做 class_terminate 您可以在中全局监视和捕获异常。然后有可能改变你的脚本语言。。。

        8
  •  1
  •   Peter Mortensen Pieter Jan Bonestroo    15 年前

    有些场景并不总是允许开发人员切换脚本语言。

    我的首选绝对是JavaScript(我在新项目中使用过它)。然而,维护旧项目仍然是必需的。不幸的是,这些都是用VBScript编写的。

    推荐文章