What options are there in ASP Classic for error handling?
ASP Classic中有哪些选项可用于错误处理?
For example:
I'm using the Mail.SendMail
function but when switching on the testing server it doesn't work, which is normal. I want to test if mailing is possible, if not then continue and/or show a message.
我正在使用Mail.SendMail函数但是当打开测试服务器时它不起作用,这是正常的。我想测试邮件是否可能,如果没有,则继续和/或显示消息。
Any ideas?
8 个解决方案
#1
There are two approaches, you can code in JScript or VBScript which do have the construct or you can fudge it in your code.
有两种方法,您可以在JScript或VBScript中编写具有该构造的代码,也可以在代码中捏造它。
Using JScript you'd use the following type of construct:
使用JScript,您将使用以下类型的构造:
<script language="jscript" runat="server">
try {
tryStatements
}
catch(exception) {
catchStatements
}
finally {
finallyStatements
}
</script>
In your ASP code you fudge it by using on error resume next at the point you'd have a try and checking err.Number at the point of a catch like:
在你的ASP代码中,你可以通过在你尝试的时候使用错误恢复来捏造它,并在捕获点检查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
Regarding Wolfwyrd's anwer: "On Error Resume Next" in fact turns error handling off! Not on. On Error Goto 0 turns error-handling back ON because at the least, we want the machine to catch it if we didn't write it in ourselves. Off = leaving it to you to handle it.
关于Wolfwyrd的anwer:“On Error Resume Next”实际上关闭了错误处理!不开。 On Error Goto 0将错误处理重新打开,因为至少我们希望机器能够捕获它,如果我们不在自己编写的话。关=留给你处理它。
If you use On Error Resume Next, you need to be careful about how much code you include after it: remember, the phrase "If Err.Number <> 0 Then" only refers to the most previous error triggered.
如果你使用On Error Resume Next,你需要注意它后面包含多少代码:记住,短语“If Err.Number <> 0 Then”仅指最先触发的错误。
If your block of code after "On Error Resume Next" has several places where you might reasonably expect it to fail, then you must place "If Err.number <> 0" after each and every one of those possible failure lines, to check execution.
如果“On Error Resume Next”之后的代码块有几个地方你可能会合理地预期它会失败,那么你必须在每个可能的故障行之后放置“If Err.number <> 0”来检查执行。
Otherwise, after "on error resume next" means just what it says - your code can fail on as many lines as it likes and execution will continue merrily along. That's why it's a pain in the ass.
否则,在“on next resume next”之后意味着它所说的内容 - 你的代码可以在它喜欢的行数上失败,并且执行将继续快速执行。这就是为什么这是一个痛苦的屁股。
#3
A rather nice way to handle this for missing COM classes:
一个相当不错的方法来处理这个缺少的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
1) Add On Error Resume Next
at top of the page
1)在页面顶部添加On Error Resume Next
2) Add following code at bottom of the page
2)在页面底部添加以下代码
If Err.Number <> 0 Then
Response.Write (Err.Description)
Response.End
End If
On Error GoTo 0
#5
For anytone who has worked in ASP as well as more modern languages, the question will provoke a chuckle. In my experience using a custom error handler (set up in IIS to handle the 500;100 errors) is the best option for ASP error handling. This article describes the approach and even gives you some sample code / database table definition.
对于曾经使用ASP以及更现代语言工作的任何人来说,这个问题会引起一阵轻笑。根据我的经验,使用自定义错误处理程序(在IIS中设置以处理500; 100错误)是ASP错误处理的最佳选择。本文介绍了该方法,甚至为您提供了一些示例代码/数据库表定义。
#6
the statement On Error Resume Next should be placed on top of what we want to validate.
On Error Resume Next语句应置于我们想要验证的范围之上。
On Error Resume Next
'Your code logic is here
Then end with statement like:
然后以如下声明结束:
If Err.Number <> 0 then
'Your error message goes here'
End if
#7
Been a while since I was in ASP land, but iirc there's a couple of ways:
从我在ASP的土地上待了一段时间,但是iirc有几种方法:
try catch finally
can be reasonably simulated in VBS (good article
here
here) and there's an event called class_terminate
you can watch and catch exceptions globally in. Then there's the possibility of changing your scripting language...
try catch终于可以在VBS中合理模拟了(这里的好文章)并且有一个名为class_terminate的事件,你可以在全局范围内观察和捕获异常。然后就有可能改变你的脚本语言......
#8
Some scenarios don't always allow developers to switch scripting language.
有些场景并不总是允许开发人员切换脚本语言。
My preference is definitely for JavaScript (and I have used it in new projects). However, maintaining older projects is still required and necessary. Unfortunately, these are written in VBScript.
我的偏好绝对是JavaScript(我在新项目中使用过它)。但是,仍然需要维护旧项目。不幸的是,这些是用VBScript编写的。
So even though this solution doesn't offer true "try/catch" functionaility, the result is the same, and that's good enough for me to get the job done.
所以即使这个解决方案没有提供真正的“尝试/捕获”功能,结果也是一样的,这对我来说已经足够好了。
#1
There are two approaches, you can code in JScript or VBScript which do have the construct or you can fudge it in your code.
有两种方法,您可以在JScript或VBScript中编写具有该构造的代码,也可以在代码中捏造它。
Using JScript you'd use the following type of construct:
使用JScript,您将使用以下类型的构造:
<script language="jscript" runat="server">
try {
tryStatements
}
catch(exception) {
catchStatements
}
finally {
finallyStatements
}
</script>
In your ASP code you fudge it by using on error resume next at the point you'd have a try and checking err.Number at the point of a catch like:
在你的ASP代码中,你可以通过在你尝试的时候使用错误恢复来捏造它,并在捕获点检查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
Regarding Wolfwyrd's anwer: "On Error Resume Next" in fact turns error handling off! Not on. On Error Goto 0 turns error-handling back ON because at the least, we want the machine to catch it if we didn't write it in ourselves. Off = leaving it to you to handle it.
关于Wolfwyrd的anwer:“On Error Resume Next”实际上关闭了错误处理!不开。 On Error Goto 0将错误处理重新打开,因为至少我们希望机器能够捕获它,如果我们不在自己编写的话。关=留给你处理它。
If you use On Error Resume Next, you need to be careful about how much code you include after it: remember, the phrase "If Err.Number <> 0 Then" only refers to the most previous error triggered.
如果你使用On Error Resume Next,你需要注意它后面包含多少代码:记住,短语“If Err.Number <> 0 Then”仅指最先触发的错误。
If your block of code after "On Error Resume Next" has several places where you might reasonably expect it to fail, then you must place "If Err.number <> 0" after each and every one of those possible failure lines, to check execution.
如果“On Error Resume Next”之后的代码块有几个地方你可能会合理地预期它会失败,那么你必须在每个可能的故障行之后放置“If Err.number <> 0”来检查执行。
Otherwise, after "on error resume next" means just what it says - your code can fail on as many lines as it likes and execution will continue merrily along. That's why it's a pain in the ass.
否则,在“on next resume next”之后意味着它所说的内容 - 你的代码可以在它喜欢的行数上失败,并且执行将继续快速执行。这就是为什么这是一个痛苦的屁股。
#3
A rather nice way to handle this for missing COM classes:
一个相当不错的方法来处理这个缺少的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
1) Add On Error Resume Next
at top of the page
1)在页面顶部添加On Error Resume Next
2) Add following code at bottom of the page
2)在页面底部添加以下代码
If Err.Number <> 0 Then
Response.Write (Err.Description)
Response.End
End If
On Error GoTo 0
#5
For anytone who has worked in ASP as well as more modern languages, the question will provoke a chuckle. In my experience using a custom error handler (set up in IIS to handle the 500;100 errors) is the best option for ASP error handling. This article describes the approach and even gives you some sample code / database table definition.
对于曾经使用ASP以及更现代语言工作的任何人来说,这个问题会引起一阵轻笑。根据我的经验,使用自定义错误处理程序(在IIS中设置以处理500; 100错误)是ASP错误处理的最佳选择。本文介绍了该方法,甚至为您提供了一些示例代码/数据库表定义。
#6
the statement On Error Resume Next should be placed on top of what we want to validate.
On Error Resume Next语句应置于我们想要验证的范围之上。
On Error Resume Next
'Your code logic is here
Then end with statement like:
然后以如下声明结束:
If Err.Number <> 0 then
'Your error message goes here'
End if
#7
Been a while since I was in ASP land, but iirc there's a couple of ways:
从我在ASP的土地上待了一段时间,但是iirc有几种方法:
try catch finally
can be reasonably simulated in VBS (good article
here
here) and there's an event called class_terminate
you can watch and catch exceptions globally in. Then there's the possibility of changing your scripting language...
try catch终于可以在VBS中合理模拟了(这里的好文章)并且有一个名为class_terminate的事件,你可以在全局范围内观察和捕获异常。然后就有可能改变你的脚本语言......
#8
Some scenarios don't always allow developers to switch scripting language.
有些场景并不总是允许开发人员切换脚本语言。
My preference is definitely for JavaScript (and I have used it in new projects). However, maintaining older projects is still required and necessary. Unfortunately, these are written in VBScript.
我的偏好绝对是JavaScript(我在新项目中使用过它)。但是,仍然需要维护旧项目。不幸的是,这些是用VBScript编写的。
So even though this solution doesn't offer true "try/catch" functionaility, the result is the same, and that's good enough for me to get the job done.
所以即使这个解决方案没有提供真正的“尝试/捕获”功能,结果也是一样的,这对我来说已经足够好了。