作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 <cftry
标签之外有一个 <cfmail
。在 <cftry
中设置了一个变量 x。变量 x 不会超过 </cftry>
。
<cfoutput>
<cftry>
<cfmail
from = "user@example.org"
to = "other@example.org"
password = "something"
username = "user@example.org"
server = "localhost"
replyto = "user@example.org"
subject = "try-catch"
type = "html" >
<cfset x = 'abc'>
this is to test email
</cfmail>
success
<cfcatch>
<cfoutput> email failed </cfoutput>
</cfcatch
</cftry>
<!--- there is no variable x --->
x is #x#
</cfoutput>
<cftry
结束后获取 x 的值。我尝试在
<cftry
中使用不同的范围设置它
<cfset register.x = 'abc'> or even
<cfset session.x = 'abc'>
<cftry>
之外保留 x 。有人可以建议一种方法来保留 x 超出
</cftry>
吗?
最佳答案
看起来您对异常处理有误解。 try
中的代码只有在没有异常的情况下才会完全执行。一旦 try
中发生异常,执行就会停止并跳转到 catch
。
示例 1
<cftry>
<cfset x = "everything is ok">
<cfcatch>
<cfset x = "an exception occured">
</cfcatch>
</cftry>
<cfoutput>#x#</cfoutput>
这将始终输出
everything is ok
,因为
try
中的代码可以执行而不会导致异常。
<cftry>
<cfthrow message="I fail you!">
<cfset x = "everything is ok">
<cfcatch>
<cfset x = "an exception occured">
</cfcatch>
</cftry>
<cfoutput>#x#</cfoutput>
这将始终输出
an exception occured
,因为
try
中的代码只执行到抛出异常的点(我们在这里使用
<cfthrow>
故意这样做)。
<cftry>
<cfset x = "everything is ok">
<cfthrow message="I fail you!">
<cfcatch>
<cfset x = "an exception occured">
</cfcatch>
</cftry>
<cfoutput>#x#</cfoutput>
这仍然会输出
an exception occured
。虽然
<cfset x = "everything is ok">
语句被正确执行并设置了变量
x
,但由于抛出异常,我们仍然跳转到
catch
。
<cftry>
<cfthrow message="I fail you!">
<cfset x = "everything is ok">
<cfcatch>
<!--- we are doing nothing --->
</cfcatch>
</cftry>
<cfoutput>#x#</cfoutput>
这将抛出一个运行时错误,告诉您
x
未定义。为什么?因为由于遇到异常,永远不会到达声明
x
的语句。并且 catch 也不会引入变量。
<cfmail>
导致异常并且永远无法到达
<cfset x = 'abc'>
。
<cfoutput> email failed </cfoutput>
你的方式摆脱它,表现出你不在乎。记录异常(有
<cflog>
)并监视它。出于调试目的,您可以在
<cfrethrow>
中使用
<cfcatch>
来保留原始异常,而不是默默地吸收错误的真正原因。
关于cftry 中的冷聚变变量不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55523538/
我想做一些类似 Jar cvf file.jar *.class 的事情,但是我在我的 jre6 java 安装中没有看到 jar 编译器文件。 jar 是独立的东西吗?我想安装? 最佳答案 您需要下
阅读时 an intro to rxjs我读了以下内容并有点担心 the second subscription will restart the sequence from the first va
如何将 MainEngine Observable 转换为 Cold?来自这个例子: public IObservable MainEngine { get
我最近一直在进行冷迁移...这意味着我无法在进行迁移时从应用程序级别读取/写入数据库(维护页面)。 这样就不会因为更改结构而发生错误,而且如果负载很大,我也不希望 mysql 在迁移过程中崩溃。 我的
我需要在 Linux 中以物理方式关闭 PCI 设备的电源。我已经找到了我需要的函数,但似乎编写了一个内核模式应用程序来使用该库,因为我在内核头文件中找到了它。 我在 drivers/pci/pci.
我是一名优秀的程序员,十分优秀!