什么是“扔”?

发布于 2024-07-14 03:57:29 字数 45 浏览 12 评论 0原文

谁能解释一下异常处理中 throw 的用法吗? 当我抛出异常时会发生什么?

Can anyone please explain me use of throw in exception handling?
What happens when i throw an exception?

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

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

发布评论

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

评论(5

如此安好 2024-07-21 03:57:30

它的意思是“引起”异常。 当您“抛出”异常时,您是在说“出了问题,这里有一些详细信息”。

然后,您可以“捕获”“抛出”的异常,以允许您的程序正常降级,而不是出错和死亡。

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.

紫竹語嫣☆ 2024-07-21 03:57:30

“抛出”异常是触发整个异常处理过程的原因。

在正常执行过程中,程序中的各行通过循环和分支顺序执行。 当发生某种错误时,会创建并抛出异常。

抛出的异常将修改程序中通常的操作顺序,使得在某处的“catch”块内处理该异常之前不会执行“正常”指令。 一旦异常在 catch 块中被捕获,并且该 catch 块中的代码被执行(“处理”异常),正常的程序执行将在 catch 块之后立即恢复。

// Do some stuff, an exception thrown here won't be caught.
try
{
  // Do stuff
  throw new InvalidOperationException("Some state was invalid.");
  // Nothing here will be executed because the exception has been thrown
}
catch(InvalidOperationException ex) // Catch and handle the exception
{
  // This code is responsible for dealing with the error condition
  //   that prompted the exception to be thrown.  We choose to name
  //   the exception "ex" in this block.
}
// This code will continue to execute as usual because the exception
//   has been handled.

"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.

// Do some stuff, an exception thrown here won't be caught.
try
{
  // Do stuff
  throw new InvalidOperationException("Some state was invalid.");
  // Nothing here will be executed because the exception has been thrown
}
catch(InvalidOperationException ex) // Catch and handle the exception
{
  // This code is responsible for dealing with the error condition
  //   that prompted the exception to be thrown.  We choose to name
  //   the exception "ex" in this block.
}
// This code will continue to execute as usual because the exception
//   has been handled.
终难愈 2024-07-21 03:57:30

当您抛出异常时,您基本上是在说某些情况已经发生,超出了调用者处理该异常的合理手段。 它们在构造函数中特别有用,因为构造函数无法发出任何形式的构造失败信号(因为它们没有返回值)。

当您引发异常时,运行时会沿着执行链向上移动,直到找到可分配给您引发的异常类型的 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.

眼藏柔 2024-07-21 03:57:30

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.

苏大泽ㄣ 2024-07-21 03:57:30

抛出异常会导致异常在堆栈中上升。 抛出有两种主要场景。

  1. 具有您的代码特有的特殊条件

    if(inputVal < 0) 
      { 
          throw new LessThanZeroCustomException("您不能输入小于零的值"); 
      } 
      

    上面的代码假设您已经编写了一个名为 LessThanZeroCustomException 的异常对象。 我实际上不会将其命名为此名称,但名称中的 Custom 旨在说明您对此进行了编码。 它很可能继承自

  2. 具有已捕获并需要重新抛出的异常情况。 出现这种情况的正常原因是日志记录。 在大多数情况下,我不喜欢这种模式,因为你最终会花时间一遍又一遍地捕捉、记录和投掷。 这是因为大多数人在每个级别都使用此模式 try ... catch。 糟糕!

简而言之,抛出的意思是“我发现了一个无法处理的异常情况,因此我通过抛出异常来让使用此代码的人知道”。

Throwing an exception causes the exception to rise up the stack. There are two primary scenarios for a throw.

  1. Have an exceptional condition unique to your code

    if(inputVal < 0)
    {
        throw new LessThanZeroCustomException("You cannot enter a value less than zero");
    }
    

    The above code assumes you have coded an exception object called LessThanZeroCustomException. I would not actually name it this, but the Custom in the name is designed to illustrate you coded this. It would most likely inherit from

  2. Have 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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文