cftry 中的 coldfusion 变量不会持续存在
coldfusion variables within cftry do not persist
我在 <cfmail
标签之外有一个 <cftry
。在 <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。有人可以建议一种在 </cftry>
之外保留 x 的方法吗?
看来您对异常处理有误解。 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
内的代码可以执行而不会导致异常。
示例 2
<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>
做的) .
示例 3
<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
。
示例 4(这是您的问题!)
<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>
摆脱它,表现得好像你不在乎。记录异常(<cfcatch>
内有<cflog>
for that) and monitor it. For debugging purposes, you can use <cfrethrow>
保留原来的异常,而不是默默吸收错误的真正原因。
我在 <cfmail
标签之外有一个 <cftry
。在 <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。有人可以建议一种在 </cftry>
之外保留 x 的方法吗?
看来您对异常处理有误解。 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
内的代码可以执行而不会导致异常。
示例 2
<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>
做的) .
示例 3
<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
。
示例 4(这是您的问题!)
<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>
摆脱它,表现得好像你不在乎。记录异常(<cfcatch>
内有<cflog>
for that) and monitor it. For debugging purposes, you can use <cfrethrow>
保留原来的异常,而不是默默吸收错误的真正原因。