什么是“扔”?
谁能解释一下异常处理中 throw 的用法吗? 当我抛出异常时会发生什么?
Can anyone please explain me use of throw in exception handling?
What happens when i throw an exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它的意思是“引起”异常。 当您“抛出”异常时,您是在说“出了问题,这里有一些详细信息”。
然后,您可以“捕获”“抛出”的异常,以允许您的程序正常降级,而不是出错和死亡。
It means to "cause" an exception. When you "throw" an exception you are saying "something has gone wrong, here's some details".
You can then "catch" a "thrown" exception to allow your program to degrade gracefully instead of erroring and dying.
“抛出”异常是触发整个异常处理过程的原因。
在正常执行过程中,程序中的各行通过循环和分支顺序执行。 当发生某种错误时,会创建并抛出异常。
抛出的异常将修改程序中通常的操作顺序,使得在某处的“catch”块内处理该异常之前不会执行“正常”指令。 一旦异常在 catch 块中被捕获,并且该 catch 块中的代码被执行(“处理”异常),正常的程序执行将在 catch 块之后立即恢复。
"Throwing" an exception is what triggers the entire process of exception handling.
In the course of normal execution, lines in a program are executed sequentially with loops and branches. When an error of some sort happens, an exception is created and then thrown.
A thrown exception will modify the usual order of operations in a program in such a way that no "normal" instructions will be executed until the exception is handled within a "catch" block somewhere. Once an exception is caught in a catch block, and the code within that catch block is executed ("Handling" the exception), normal program execution will resume immediately following the catch block.
当您抛出异常时,您基本上是在说某些情况已经发生,超出了调用者处理该异常的合理手段。 它们在构造函数中特别有用,因为构造函数无法发出任何形式的构造失败信号(因为它们没有返回值)。
当您引发异常时,运行时会沿着执行链向上移动,直到找到可分配给您引发的异常类型的 catch 块。 它在您可能拥有的任何 finally 块中运行代码,这允许您(通常)释放您可能获得的任何资源。
When you throw an exception you're basically saying that some condition has happened beyond the reasonable means of the caller being expected to handle it. They're especially useful in constructors which have no way of signaling any form of construction failure (as they don't have return values).
When you throw an exception the runtime moves up the execution chain until it finds a catch block that is assignable to the type of exception you've thrown. On the way it runs the code in any finally blocks you may have, which allows you to (typically) release any resources you may have acquired.
throw 创建要处理的异常。 然后,您传递的对象将成为描述异常的数据。
在抛出某些东西之前,没有任何异常需要处理。
The throw creates the exception to be handled. The object you passed then becomes the data that describes the exception.
Until something is thrown there is no exception to be handled.
抛出异常会导致异常在堆栈中上升。
抛出
有两种主要场景。具有您的代码特有的特殊条件
上面的代码假设您已经编写了一个名为
LessThanZeroCustomException
的异常对象。 我实际上不会将其命名为此名称,但名称中的Custom
旨在说明您对此进行了编码。 它很可能继承自具有已捕获并需要重新抛出的异常情况。 出现这种情况的正常原因是日志记录。 在大多数情况下,我不喜欢这种模式,因为你最终会花时间一遍又一遍地捕捉、记录和投掷。 这是因为大多数人在每个级别都使用此模式
try ... catch
。 糟糕!简而言之,抛出的意思是“我发现了一个无法处理的异常情况,因此我通过抛出异常来让使用此代码的人知道”。
Throwing an exception causes the exception to rise up the stack. There are two primary scenarios for a
throw
.Have an exceptional condition unique to your code
The above code assumes you have coded an exception object called
LessThanZeroCustomException
. I would not actually name it this, but theCustom
in the name is designed to illustrate you coded this. It would most likely inherit fromHave an exceptional condition that has been caught and needs to be rethrown. The normal reason for this is logging. In most cases, I dislike this pattern, as you end up spending time catching, logging and throwing over and over again. This is because most people doing this pattern
try ... catch
at every level. Yuck!In short,
throw
means "I found an exceptional condition I cannot handle, so I am letting the person using this code know by throwing an exception".