简体   繁体   English

如何在“try ... catch”块中抛出一个未处理的异常?

[英]How can code in a “try…catch” block throw an unhandled exception?

I had an exception in some code today: "A [some exception] was unhandled." 我今天在一些代码中有一个例外:“[某些例外]未得到处理。”

However, this code was clearly inside the "try" block of a "try/catch" structure. 但是,这段代码显然位于“try / catch”结构的“try”块中。

What am I missing here? 我在这里错过了什么?

Update: It's C# 更新:这是C#

Update: Oh, forget it. 更新:哦,忘了。 It turns out the specific mechanism of error is that I'm an idiot. 事实证明,错误的具体机制是我是个白痴。 There's no fix for this. 对此没有任何解决方法。

Does the catch statement specify a specific type of exception? catch语句是否指定了特定类型的异常?

If it does, it will only catch that type of exception. 如果是,它将只捕获该类型的异常。

Unmanaged exceptions will not be caught by catch(Exception e),you can try a catch(异常e)不会捕获非托管异常,你可以试试

    try
    {
    }
    catch
    {
    }

instead of 代替

        try
        {
        }
        catch (Exception e)
        {
        }

some problems caused by Recursion such as StackOverFlow exceptions and the like will throw inside of try...catch blocks because they are not actually thrown from any particular line of code within the block, but rather by the CLR. 由递归引起的一些问题,例如StackOverFlow异常等会抛出try ... catch块,因为它们实际上并不是从块内的任何特定代码行抛出,而是由CLR抛出。 This is also true for Memory out of range exceptions and other problems that aren't the direct result of any one line of code. 对于内存超出范围的异常和其他不是任何一行代码直接导致的问题,情况也是如此。

Maybe you're talking about something like this: 也许你在谈论这样的事情:

替代文字

Were you running in a debugger with "break on exceptions"/"break on thrown" switched on? 你是否在调试器中运行“异常中断”/“抛出中断”? In this case you'll see the exception before it is passed to the try/catch. 在这种情况下,您将在传递给try / catch之前看到异常。

I have 10 dollars that says its a ThreadAbortException or some other self-throwing exception. 我有10美元说它是一个ThreadAbortException或其他一些自我抛出的异常。 If that is the case you must catch the exception twice. 如果是这种情况,您必须捕获两次异常。

在不知道语言的情况下很难说,但许多语言都有无法捕获的异常概念 - 例如在.NET中,无法捕获OutOfMemoryException和ExecutionEngineException(以及其他语言),因为它们基本上是不可恢复的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM