我们可以从catch块抛出异常吗? [英] Can we throw exception from catch block ?

查看:160
本文介绍了我们可以从catch块抛出异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了这个问题,也回答了,但不明白.

I found this question and also answer but didn''t understand that.

报价:

是.
在定义的catch块中无法处理的异常被抛出给其调用者.

Yes.
The exceptions which cant be handled in the defined catch block are thrown to its caller.


catch

{

    throw;

    // to throw specific exception

    // throw new ArgumentException("this Error occured");

}



谁能帮我理解这一点.

预先感谢.



Can anyone please help me to understand this.

Thanks in advance.

推荐答案

请参阅我对问题的评论.

您的报价可能会让您感到困惑,这纯粹是愚蠢的.没有扔给调用者"之类的东西,因为没有例外没有调用者.要了解它们是如何工作的,您应该了解调用堆栈是如何工作的.每个线程中只有一个调用堆栈.这是调用方法和传递参数的唯一机制.输入try块时,将记住当前线程的堆栈点.如果您输入另一个try块内部人员,则会记住另一个尝试点以及所有需要的执行上下文.所有这些尝试点都收集在单独的堆栈"try stack"中,即尝试点的集合.这些点的位置纯粹是运行时实体.引发异常时,系统会在堆栈上找到最接近的尝试点,并将调用堆栈恢复到该点.

这就像一台时光机.由于从未抛出异常,因此异常机制将执行时间跳回到了最后记住的点.然后,您可以处理它并重新抛出.如果重新抛出,则执行将跳回到上一个尝试点,直到调用堆栈的顶部.这就是为什么在每个线程的堆栈顶部捕获所有异常很重要.

对于习惯了常规呼叫/返回机制的人员而言,要深入了解这不是那么容易.异常和线程是在调用栈"上工作的机制.它以一种安全的方式破坏了堆栈规则,仅在时间"内跳回,并恢复了当执行进入尝试"时在过去的瞬间所留下的完整的执行上下文.无法理解此机制会导致开发人员过度处理异常.异常机制的设计目的是创建一个抛弃式"系统,非常适合于将常规"行为与异常"行为隔离开来.大多数开发活动应在假设永不发生异常情况的情况下进行,因为可以将它们与常规"行为分离地进行单独处理.

—SA
Please see my comment to the question.

You are probably confused by your quote, which is purely idiotic. There is not such thing as "throw to the caller", because exceptions do no have caller. To understand how they work, you should understand how a call stack works. You have one call stack in each thread; and this is the only mechanism of calling methods and passing parameters. When you enter a try block, the point of the stack is remembered for the current thread. If, you enter another try block insider, another try point is remembered, with all needed execution context. All those try points are collected in a separate stack "try stack", the collection of try points. The position of those points is purely a run-time entity. When an exception is thrown, the systems find the closest try point up the stack and restores the call stack to this point.

This is like a time machine. The exception mechanism jumps execution back in time to the last remembered point as the exception was never thrown. Then you can handle it and re-throw. If you re-throw, the execution jumps back the the previous try point, up to the very top of the call stack. That''s why it is important to catch all exceptions on top of the stack of each thread.

This is not so easy to understand in depth for a person who is used to regular call/return mechanism. Exception, as well as threading, is the mechanism working "over" the call stack; it breaks the stack rules in a safe way, jumping only back in "time" and restoring complete execution context as it was left at the past moment of time, when execution enters "try". Failure to understand this mechanism cause developers to over-process exception. Exception mechanism was designed to create a "throw-and-forget" system, very good for isolation of "regular" behavior from the "exceptional" one. Most of the development activity should be made in assumption that exceptional situations never happen, because they can be handled separately, in isolation from the "regular" behavior.

—SA


这篇关于我们可以从catch块抛出异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆