现实世界中应该如何使用 throw、try{}catch{}?

发布于 2024-12-07 10:43:18 字数 932 浏览 1 评论 0原文

我的意思是,我知道有关 throw、try {} catch {} 的所有语言规则,但我不确定在现实世界中是否正确使用它们。请看下面的例子:

我们有一大段科学代码,可以完成各种图像处理工作,最近我们决定对其进行修饰并使其更加健壮。经常使用的例程之一是voidrotate_in_place(float* image, image_size sz)

为了使其更加健壮,我们在代码开头添加了一些健全性检查:

void rotate_in_place(float* image, image_size sz) {
    // rotate_in_place does not support non-square image;
    if (sz.nx != sz.ny)  throw NonSquareImageError;
    // rotate_in_place does not support image too small or too large
    if (sz.nx <= 2 || sz.nx > 1024)  throw WrongImageSizeError;
    // Real rode here
    .....
}

现在的问题是,rotate_in_place() 在 1000 多个地方使用,我应该用 try{} catch {} 包装对rotate_in_place() 的每个调用吗?在我看来,这会让代码变得异常臃肿。另一种可能性是不包装任何 try{} catch{} 并让程序退出,但这与仅使用有什么不同

if (sz.nx != sz.ny) {
    cerr << "Error: non-squared image error!\n";
    exit(0);
}

简而言之,我不太确定使用 throw、try、catch 的真正好处,有什么好的建议吗?

I mean, I knew all the language rules about throw, try {} catch {}, but I am not sure if I am using them correctly in the real world. Please see the following example:

We have a large piece of scientific code which did all sorts of image processing things, recently we decided to spruce it up and make it more robust. One of the routines which is frequently used is void rotate_in_place(float* image, image_size sz);

To make it more robust, we add some sanity check at the beginning of the code:

void rotate_in_place(float* image, image_size sz) {
    // rotate_in_place does not support non-square image;
    if (sz.nx != sz.ny)  throw NonSquareImageError;
    // rotate_in_place does not support image too small or too large
    if (sz.nx <= 2 || sz.nx > 1024)  throw WrongImageSizeError;
    // Real rode here
    .....
}

Now the problem is that rotate_in_place() is used in over 1000 places, shall I wrap each call of rotate_in_place() with try{} catch {}, this looks to me will make code incredibly bloated. Another possibility is do not wrap any try{} catch{} and let the program exit, but how is this different from just using

if (sz.nx != sz.ny) {
    cerr << "Error: non-squared image error!\n";
    exit(0);
}

In short, I am not so sure about the real benefit of using throw, try, catch, any good suggestions?

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

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

发布评论

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

评论(8

抹茶夏天i‖ 2024-12-14 10:43:18

每个处理错误的站点都需要 try-catch 块。这一切都取决于您的设计,但我怀疑您是否需要处理每个 rotate_in_place 调用站点中的错误,大多数时候您可能会避免向上传播。

打印错误并使用 exit 是不好的,原因有以下三个:

  1. 您无法处理错误。 exit 不进行处理(除非当错误绝对严重时完成,但你的函数无法知道这一点 - 调用者可能有办法恢复)。
  2. 您通过写入硬编码流来扩展该函数的职责,该流甚至可能不可用(这是 rotate_in_place,而不是 rotate_in_place_and_print_errors_and_kill_the_program_if_something_is_wrong) - 这会损害可重用性。
  3. 使用这种方法您会丢失所有调试信息(您可以从未处理的异常生成堆栈跟踪,您无法对每次都会退出的函数执行任何操作 - 未处理的异常是一个错误,但您可以跟踪它的源代码) 。

Every site that handles the error needs try-catch block. It all depends on your design, but I doubt you need to handle the error in every rotate_in_place call-site, you probably get away from propagating upwards most of the time.

Printing the error and using exit is bad for three reasons:

  1. You can't handle the error. exit is not handling (unless it's done when the error is absolutely critical, but your function cannot know that — caller might have a way to recover).
  2. You're extending responsibilities of the function with writing to a hard-coded stream, which might not even be available (this is rotate_in_place, not rotate_in_place_and_print_errors_and_kill_the_program_if_something_is_wrong) — this hurts reusability.
  3. You lose all debugging information with this approach (you can generate stack traces from unhandled exceptions, you can't do anything with a function that bails out every time — unhandled exception is a bug, but it's a bug you can follow to the source).
嘴硬脾气大 2024-12-14 10:43:18

例外的一般规则是,“直接调用站点是否关心这里发生了什么?”如果调用站点确实关心,那么返回状态代码可能是有意义的。否则,投掷更有意义。

这样考虑——当然,您的就地旋转方法有几个无效的参数类型,在这种情况下您可能应该抛出std::invalid_argument。例如,rotate_in_place 的调用者不太可能想要处理或知道如何处理图像不是正方形的情况,因此最好将其表达为异常。

另一种可能性是不包装任何 try{} catch{} 并让
程序退出,但这与仅使用有何不同

if (sz.nx != sz.ny) {
    赛尔<< "错误:非平方图像错误!\n";
    退出(0);
}

它有何不同?不同之处在于,如果后来有人想要获取您的函数并将其放入 GUI 应用程序中,他们不必根据错误终止程序。他们可以将该异常变成对用户来说很漂亮的东西或类似的东西。

它现在对您也有好处 - 即您不必将 拉入该翻译单元只是为了进行错误写入。

我通常使用这样的模式:

int realEntryPoint()
{
    //Program goes here
}

int main()
{
    //Allow the debugger to get the exception if this is a debug binary
    #ifdef NDEBUG
    try
    #endif
    {
      return realEntryPoint();
    }
    #ifdef NDEBUG
    catch (std::exception& ex)
    {
      std::cerr << "An exception was thrown: " << ex.what() << std::endl;
    }
    #endif
}

The general rule for exceptions is, "Does the immediate call site care about what's going on here?" If the call site does care, then returning a status code probably makes sense. Otherwise, throwing makes more sense.

Consider it this way -- sure, your rotate in place method has a couple of invalid argument types, in which case you should probably throw std::invalid_argument. It's unlikely that a caller of rotate_in_place wants to deal with or knows how to deal with the case that an image was not square, for example, and therefore that's probably better expressed as an exception.

Another possibility is do not wrap any try{} catch{} and let the
program exit, but how is this different from just using

if (sz.nx != sz.ny) {
    cerr << "Error: non-squared image error!\n";
    exit(0);
}

It's different because if someone later wants to take your function and put it in, say, a GUI application, they don't have to terminate the program based on the error. They can turn that exception into something pretty for the user or something like that.

It also has benefits for you right now -- namely that you don't have to pull <iostream> into that translation unit simply to do error writing.

I usually use a pattern something like this:

int realEntryPoint()
{
    //Program goes here
}

int main()
{
    //Allow the debugger to get the exception if this is a debug binary
    #ifdef NDEBUG
    try
    #endif
    {
      return realEntryPoint();
    }
    #ifdef NDEBUG
    catch (std::exception& ex)
    {
      std::cerr << "An exception was thrown: " << ex.what() << std::endl;
    }
    #endif
}
悲念泪 2024-12-14 10:43:18

现在的问题是,rotate_in_place() 在 1000 多个地方使用,我应该用 try{} catch {} 包装 rotate_in_place() 的每个调用吗?在我看来会使代码变得异常臃肿。

它会的,而且它首先就违背了使用异常的目的。

另一种可能性是不包装任何 try{} catch{} 并让程序退出,但这与仅使用 [...] 有何不同

,您以后可以随时更改异常处理的位置。如果在某个时候您找到了一个更好的位置来明智地处理错误(也许可以从中恢复),那么这就是您放置 catch 的地方。有时,这就是您抛出异常的函数;有时它位于调用链的上游。

以防万一,请在 main 中放置一个 catch-all 。从标准异常(例如 std::runtime_error)派生异常使得执行此操作变得更加容易。

Now the problem is that rotate_in_place() is used in over 1000 places, shall I wrap each call of rotate_in_place() with try{} catch {}, this looks to me will make code incredibly bloated.

It will, and it beats the purpose of using exceptions in the first place.

Another possibility is do not wrap any try{} catch{} and let the program exit, but how is this different from just using [...]

That you can always change the location of exception handling later on. If at some point you find a better place to sensibly handle the error (perhaps recovering from it), then that's the point where you put the catch. Sometimes that's in the very function where you throw the exception; sometimes it's way up in the call chain.

Do put a catch-all in main, just in case. Deriving exceptions from standard ones such as std::runtime_error makes doing this a lot easier.

偏爱自由 2024-12-14 10:43:18

使用异常处理的要点在于遵循以下简单规则:

  • 一旦由于错误的用户输入而发生任何错误(内部逻辑应通过断言/日志记录来处理),则抛出异常。尽可能快地抛出:与 .Net 异常相比,C++ 异常通常相当便宜。
  • 如果您无法处理错误,则让异常传播。这意味着几乎总是如此。

要记住的是:异常应该冒泡到可以处理的程度。这可能意味着出现带有某种错误格式的对话框,或者这可能意味着某些不重要的逻辑片段最终不会被执行,等等。

The point in using exception handling holds in following simple rules:

  • As soon as anything bad can happen due to bad user input (internal logic should be handled via assertions/logging), throw an exception. Throw as soon as possible, and as much as possible: C++ exceptions are usually pretty cheap compared to say, .Net ones.
  • Let an exception propagate if you can't handle the error. This means pretty much always.

The thing to remember is: The exception should bubble up to the point where it can be handled. This can mean a dialog box with some formatting of the error, or this can imply that some unimportant piece of logic won't be executed after all, etc.

情话墙 2024-12-14 10:43:18

使用异常允许调用者决定如何处理错误。如果您直接在函数内调用 exit,那么程序将退出,而调用者无法决定如何处理错误。另外,使用 exit 时,堆栈对象不会被展开。 :-(

Using exceptions allows the caller to decide how to handle an error. If you called exit directly within the function, then the program would exit without the caller being able to decide how to handle the error. Also, with exit, stack objects would not be unwound. :-(

够钟 2024-12-14 10:43:18

您可以做的是,如果函数调用成功,则使rotate_in_place 返回布尔值。并通过函数参数返回旋转后的图像。

bool rotate_in_place(float* image, image_size sz, float** rotated_image) {
    // rotate_in_place does not support non-square image;
    if (sz.nx != sz.ny)  return false;
    // rotate_in_place does not support image too small or too large
    if (sz.nx <= 2 || sz.nx > 1024)  return false;
    // Real rode here
    .....
    return true;
}

What you can do is to make rotate_in_place return a boolean if the function call was succesfull. And return the rotated image via a function parameter.

bool rotate_in_place(float* image, image_size sz, float** rotated_image) {
    // rotate_in_place does not support non-square image;
    if (sz.nx != sz.ny)  return false;
    // rotate_in_place does not support image too small or too large
    if (sz.nx <= 2 || sz.nx > 1024)  return false;
    // Real rode here
    .....
    return true;
}
殤城〤 2024-12-14 10:43:18

这取决于。

异常通常是要被捕获/处理的。在您的情况下,是否可以处理异常(例如,用户提供了非方形图像,因此您要求他们重试)。但是,如果您对此无能为力,那么 cerr 就是您的最佳选择。

It depends.

Exceptions are generally meant to be caught/handled. In your case, is it possible to handle the exception (for instance, the user provides a non-square image, so you ask them to try again). However if there is nothing you can do about it, then cerr is the way to go.

匿名的好友 2024-12-14 10:43:18

好吧,我同意真正使用异常会导致代码臃肿。这是我不喜欢他们的主要原因。

无论如何,对于您的示例:抛出异常和仅使用 exit() 之间的主要区别在于,由于异常的处理发生(或应该发生)在生成错误/异常的程序片段之外,因此您可以不指定函数/类的用户必须如何处理错误。通过使用异常,您可以进行不同的处理,例如中止程序、报告错误甚至从某些错误中恢复。

TLDNR:如果使用异常,则代码的异常生成部分不需要指定如何处理异常情况。这发生在外部程序中,并且可以根据代码的使用方式进行更改。

Well, I agree that really using Exceptions results in bloated code. That is the main reason for me not liking them.

Anyway, as to your example: The key difference between throwing exceptions and just using exit() is that, since the handling of the exception happens (or is supposed to happen) outside of the program fragment that generated the error/exception, you do not specify how the user of a function/class has to handle the error. By using exceptions you allow different treatments like aborting the program, reporting errors or even recovering from certain errors.

TLDNR: If you use exceptions, the exception-generating part of the code does not need to specify how the exceptional case is treated. That happens in the outside program and can be changed depending on how the code is being used.

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