English 中文(简体)
本表的要点是什么?
原标题:What is the point of this Catch statement?
  • 时间:2009-09-22 17:12:09
  •  标签:

我在遗产法典中看到这一点。 如果是的话,单行的用意是什么?

       Try
            Some Oracle access statement
       Catch err As OracleClient.OracleException
            Throw
       Finally
           Do something
       End Try

结果是否与原始错误没有被抓住一样? 您是否使用“Throw声明”的参数重新计算最初的错误,通常是在先采取行动之后?

最佳回答

它认为它正被用来掩盖错误。 虽然通常在渔获量说明中比仅仅扔 throw(如伐木、警报等)还要多,但在这种情况下,允许最后声明用于做一些清理,然后将错误推到下一个水平。 如果没有尝试/捕获物,这一清理法如何在这种范围内书写? 正如有人已经提到的那样,扔 throw物(没有外衣)正在保留ack痕。

问题回答

我经常在偷窃时使用这种模式;我打着在扔 throw声明上的突破点,以便我能够检查<代码>r。 不能确定这样做有另一个良好理由。

许多人认为这是一个禁忌,但不是这样。 如果是:

Module Module1
    Function Filter() As Boolean
        Console.WriteLine("1")
        Return True
    End Function

    Sub Thrower()
        Try
            Throw New Exception("x")
        Finally
            Console.WriteLine("3")
        End Try
    End Sub

    Sub NoCatch()
        Try
            Thrower()
        Finally
            Console.WriteLine("2")
        End Try
    End Sub

    Sub WithCatch()
        Try
            Thrower()
        Catch ex As Exception
            Throw
        Finally
            Console.WriteLine("2")
        End Try
    End Sub

    Sub Caller(ByVal method As Action)
        Try
            method()
        Catch ex As Exception When Filter()
        End Try
    End Sub

    Sub Main()
        Console.WriteLine("No Catch")
        Caller(AddressOf NoCatch)
        Console.WriteLine("With Catch")
        Caller(AddressOf WithCatch)
    End Sub
End Module

产出

No Catch
1
3
2
With Catch
3
1
2

Edit: One scenario when this actually matters this: The Thrower and NoCatch functions are in the same assembly, which is thrusted. The Caller method is not trusted and malicious. Now imagine that the Thrower method uses WindowsIdentity to impersonate another user, which the untrusted assembly is not allowed to do. It then relies on a using block (= try/finally) to unimpersonate the user, but it throws an exception. This would mean that the malicious assembly runs as the impersonated user during the execution of the Filter method. Perhaps this will also work if asserting permissions, but I m not sure.

这丝毫没有。 增长是正确的。

你可以删除渔获量,但如果在最后有一个以上评论的话,你想要离开审判。

之后,该错误又再次投下。 我看不到这一点,因为你可以做Try......。 最后没有加油条款......

jenningj虽然有好点,但本来可以用来用断点检查错误,但不应出现在生产法中。

在无参数的情况下使用 throw弹的主要原因是,它将保留原子弹。

同样,在把一个断点放在扔 throw物的声明中,你本可以改变你的录像带,以打破神奇的例外情况,从而更容易地在任何地方增加断点。

这将重新出现同样的错误。 它将允许你在把错误 throw倒给打电话者的同时,在最后部分工作。

http://www.ohchr.org。

很显然,没有捕获和再种植的特有泡沫,因此不需要。 我的立场是更正的。





相关问题
热门标签