现实世界中应该如何使用 throw、try{}catch{}?
我的意思是,我知道有关 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
每个处理错误的站点都需要
try
-catch
块。这一切都取决于您的设计,但我怀疑您是否需要处理每个rotate_in_place
调用站点中的错误,大多数时候您可能会避免向上传播。打印错误并使用
exit
是不好的,原因有以下三个:exit
不进行处理(除非当错误绝对严重时完成,但你的函数无法知道这一点 - 调用者可能有办法恢复)。rotate_in_place
,而不是rotate_in_place_and_print_errors_and_kill_the_program_if_something_is_wrong
) - 这会损害可重用性。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 everyrotate_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: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).rotate_in_place
, notrotate_in_place_and_print_errors_and_kill_the_program_if_something_is_wrong
) — this hurts reusability.例外的一般规则是,“直接调用站点是否关心这里发生了什么?”如果调用站点确实关心,那么返回状态代码可能是有意义的。否则,投掷更有意义。
这样考虑——当然,您的就地旋转方法有几个无效的参数类型,在这种情况下您可能应该抛出
std::invalid_argument
。例如,rotate_in_place
的调用者不太可能想要处理或知道如何处理图像不是正方形的情况,因此最好将其表达为异常。它有何不同?不同之处在于,如果后来有人想要获取您的函数并将其放入 GUI 应用程序中,他们不必根据错误终止程序。他们可以将该异常变成对用户来说很漂亮的东西或类似的东西。
它现在对您也有好处 - 即您不必将
拉入该翻译单元只是为了进行错误写入。我通常使用这样的模式:
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 ofrotate_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.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:
它会的,而且它首先就违背了使用异常的目的。
,您以后可以随时更改异常处理的位置。如果在某个时候您找到了一个更好的位置来明智地处理错误(也许可以从中恢复),那么这就是您放置
catch
的地方。有时,这就是您抛出异常的函数;有时它位于调用链的上游。以防万一,请在
main
中放置一个catch
-all 。从标准异常(例如std::runtime_error
)派生异常使得执行此操作变得更加容易。It will, and it beats the purpose of using exceptions in the first place.
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 inmain
, just in case. Deriving exceptions from standard ones such asstd::runtime_error
makes doing this a lot easier.使用异常处理的要点在于遵循以下简单规则:
要记住的是:异常应该冒泡到可以处理的程度。这可能意味着出现带有某种错误格式的对话框,或者这可能意味着某些不重要的逻辑片段最终不会被执行,等等。
The point in using exception handling holds in following simple rules:
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.
使用异常允许调用者决定如何处理错误。如果您直接在函数内调用
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, withexit
, stack objects would not be unwound. :-(您可以做的是,如果函数调用成功,则使rotate_in_place 返回布尔值。并通过函数参数返回旋转后的图像。
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.
这取决于。
异常通常是要被捕获/处理的。在您的情况下,是否可以处理异常(例如,用户提供了非方形图像,因此您要求他们重试)。但是,如果您对此无能为力,那么
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.好吧,我同意真正使用异常会导致代码臃肿。这是我不喜欢他们的主要原因。
无论如何,对于您的示例:抛出异常和仅使用 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.