如何向 ColdFusion 异常对象添加属性

发布于 2024-12-13 06:34:46 字数 462 浏览 1 评论 0原文

我知道这看起来一定是一个非常人为的例子,但我有一个情况,在一个 catch 中我将尝试/捕获另一个异常。但是,我想在第二个捕获中包含有关第一个异常的信息。理想情况下,它看起来像这样:

try {
  a = b;
}
catch ( any e ) {
  local.originalException = Duplicate(e);
  try  {
     throw "New exception!!!";
  }
  catch( any e )    {
     e.originalException = local.originalException;
     doNotification( e );
  }
}

当我运行这个小测试时,设置 e.originalException 时不会发生意外异常,但是,当我在设置后立即转储异常时,它不是异常结构的一部分。也许还有另一种方法可以实现这一目标吗?我用的是ACF 9。

I know this must seem like a very contrived example, but I have a case in which within a catch I will be try/catching another exception. However, I would like to include information in the second catch about the first exception. Ideally it would look something like this:

try {
  a = b;
}
catch ( any e ) {
  local.originalException = Duplicate(e);
  try  {
     throw "New exception!!!";
  }
  catch( any e )    {
     e.originalException = local.originalException;
     doNotification( e );
  }
}

When I run this little test, there is no unexpected exception that occurs when setting e.originalException, however, when I dump the exception immediately after setting it, it is not a part of the exception struct. Is there perhaps another way to achieve this? I am using ACF 9.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倒数 2024-12-20 06:34:46

不幸的是,您实际上无法将太多附加信息“附加”到 CF 异常。但是你可以做这样的事情来将一个异常包装在另一个异常中......

<cftry>
<cfset a = b>
<cfcatch>
    <cftry>
        <cfthrow message="new exception" extendedinfo="#serializeJson(cfcatch)#">
        <cfcatch>
            <cfset stOriginalExceptionData = deserializeJson(cfcatch.extendedInfo)>
            <cfdump var="#stOriginalExceptionData#">
        </cfcatch>
    </cftry>
</cfcatch>

这种技术对你有帮助吗?

You can't really "attach" much additional info to a CF exception, unfortunately. But you could do something like this to wrap up one exception in another one...

<cftry>
<cfset a = b>
<cfcatch>
    <cftry>
        <cfthrow message="new exception" extendedinfo="#serializeJson(cfcatch)#">
        <cfcatch>
            <cfset stOriginalExceptionData = deserializeJson(cfcatch.extendedInfo)>
            <cfdump var="#stOriginalExceptionData#">
        </cfcatch>
    </cftry>
</cfcatch>

Does this sort of technique help you?

执着的年纪 2024-12-20 06:34:46

您是否尝试过复制该对象?因为根据 文档,您无法修改 cfcatch 异常:

已更改的对象修改:您无法修改由
cfcatch

Did you try duplicating the object? Because according to the documentation you cannot modify cfcatch exceptions:

Changed object modification: you cannot modify the object returned by
cfcatch

檐上三寸雪 2024-12-20 06:34:46

我知道我回答“有点”晚了,但我遇到了同样的问题,这就是我解决它的方法:

<cfscript>
try {
  a = b;
}
catch ( any e ) {
  local.originalException = Duplicate(e);
  try  {
     throw "New exception!!!";
  }
  catch( any e )    {
    local.e = deserializeJson(serializeJson(e));
    local.e.originalException = local.originalException;
    writeDump(local.e);
  }
}
</cfscript>

I know I'm a "bit" late to answer, but I ran into the same issue and this is how I got around it:

<cfscript>
try {
  a = b;
}
catch ( any e ) {
  local.originalException = Duplicate(e);
  try  {
     throw "New exception!!!";
  }
  catch( any e )    {
    local.e = deserializeJson(serializeJson(e));
    local.e.originalException = local.originalException;
    writeDump(local.e);
  }
}
</cfscript>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文