何时以及如何使用异常处理?

时间:2022-03-04 20:35:17

I am reading about exception handling. I got some information about what exception handling is, but I have a few questions:

我正在阅读有关异常处理的文章。我得到一些关于什么是异常处理的信息,但是我有几个问题:

  1. When to throw an exception?
  2. 什么时候抛出异常?
  3. Instead of throwing an exception, can we use a return value to indicate the error?
  4. 我们可以使用返回值来指示错误,而不是抛出异常吗?
  5. If I protect all my functions by try-catch blocks, won't it reduce the performance?
  6. 如果我通过try-catch块来保护我的所有功能,它不会降低性能吗?
  7. When to use exception handling?
  8. 何时使用异常处理?
  9. I saw a project where each and every function in that project contained a try-catch block (i.e. code inside the entire function is surrounded by try-catch block). Is this a good practice?
  10. 我看到一个项目,该项目中的每个函数都包含一个try-catch块(即整个函数中的代码被try-catch块包围)。这是一个好习惯吗?
  11. What is the difference between try-catch and __try __except?
  12. try-catch和try- except有什么区别?

7 个解决方案

#1


66  

Here's quite comprehensive guide on exceptions that I think is a Must Read:

这里有一个关于例外的全面指南,我认为这是必须阅读的:

Exceptions and error handling - C++ FAQ or C++ FAQ lite

异常和错误处理- c++ FAQ或c++ FAQ lite

As a general rule of thumb, throw an exception when your program can identify an external problem that prevents execution. If you receive data from the server and that data is invalid, throw an exception. Out of disk space? Throw an exception. Cosmic rays prevent you from querying the database? Throw an exception. But if you get some invalid data from inside your very own program - don't throw an exception. If your problem comes from your own bad code, it's better to use ASSERTs to guard against it. Exception handling is needed to identify problems that program cannot handle and tell them about the user, because user can handle them. But bugs in your program are not something the user can handle, so program crashing will tell not much less than "Value of answer_to_life_and_universe_and_everything is not 42! This should never happen!!!!11" exception.

作为一般的经验法则,当您的程序可以识别阻止执行的外部问题时,抛出异常。如果您从服务器接收数据并且该数据无效,抛出异常。磁盘空间?抛出异常。宇宙射线阻止你查询数据库?抛出异常。但是,如果您从自己的程序中获得了一些无效的数据,请不要抛出异常。如果您的问题来自您自己的坏代码,那么最好使用assert来防止它。需要异常处理来识别程序无法处理的问题,并告诉它们关于用户的问题,因为用户可以处理它们。但是程序中的错误并不是用户可以处理的,所以程序崩溃将告诉你“答案是什么?”这应该不会发生! ! ! !11“例外。

Catch an exception where you can do something useful with it, like, display a message box. I prefer to catch an exception once inside a function that somehow handles user input. For example, user presses button "Annihilate all hunams", and inside annihilateAllHunamsClicked() function there's a try...catch block to say "I can't". Even though annihilation of hunamkind is a complex operation that requires calling dozens and dozens of functions, there is only one try...catch, because for a user it's an atomic operation - one button click. Exception checks in every function are redundant and ugly.

捕获一个异常,您可以使用它做一些有用的事情,比如显示消息框。我希望在处理用户输入的函数中捕获一次异常。例如,用户按下按钮“Annihilate all hunams”,在annihilateAllHunamsClicked()函数中有一个try…抓住block说“我不能”。尽管消灭hunamkind是一个复杂的操作,需要调用数十个函数,但只有一次尝试……catch,因为对于一个用户来说,它是一个原子操作-一个按钮点击。每个函数中的异常检查都是冗余和丑陋的。

Also, I can't recommend enough getting familiar with RAII - that is, to make sure that all data that is initialized is destroyed automatically. And that can be achieved by initializing as much as possible on stack, and when you need to initialize something on heap, use some kind of smart pointer. Everything initialized on the stack will be destroyed automatically when an exception is thrown. If you use C-style dumb pointers, you risk memory leak when an exception is thrown, because there is noone to clean them up upon exception (sure, you can use C-style pointers as members of your class, but make sure they are taken care of in destructor).

此外,我不建议您对RAII有足够的了解——也就是说,要确保所有初始化的数据都被自动销毁。这可以通过在堆栈上尽可能多地初始化来实现,当您需要在堆上初始化一些东西时,使用某种智能指针。当抛出异常时,堆栈上初始化的所有内容将被自动销毁。如果使用c风格的dumb指针,在抛出异常时可能会导致内存泄漏,因为没有人会在异常情况下清理它们(当然,您可以使用c风格的指针作为类的成员,但是要确保它们在销毁程序中得到了处理)。

#2


10  

Exceptions are useful in a variety of circumstances.

例外在各种情况下都是有用的。

First, there are some functions where the cost of calculating the pre-condition is so high it is better to just do the calculation and abort with an exception if it is found the pre-condition is not met. For example, you cannot invert a singular matrix, however to calculate if it is singular you calculate the determinant which is very expensive: it may have to be done inside the function anyhow, so just "have a try" at inverting the matrix and report an error if you can't by throwing an exception. This is basically an exception as negative pre-condition usage.

首先,在某些函数中,计算预条件的成本太高,如果发现不满足预条件,最好只进行计算并异常终止。例如,您不能逆奇异矩阵,但是计算如果是单数计算行列式很昂贵:这可能要做内部函数无论如何,只是“试一试”反相矩阵并报告一个错误如果你不能通过抛出异常。这基本上是一个作为消极前提使用的例外。

Then there are other cases where your code is already complex and passing error information up the call chain is difficult. This is partly because C and C++ have broken data structure models: there are other, better ways, but C++ doesn't support them (such as using monads in Haskell). This use is basically I couldn't be bothered to do it right so I'll throw an exception: its not the right way but it's practical.

还有一些情况,您的代码已经很复杂,并且很难将错误信息传递到调用链上。这部分是因为C和c++破坏了数据结构模型:还有其他更好的方法,但是c++不支持它们(比如在Haskell中使用monads)。这种用法基本上是我懒得去做的,所以我要抛出一个例外:它不是正确的方法,但它是实用的。

Then there is the main use of exceptions: to report when external pre-conditions or invariants, such as sufficient resources like memory or disk space, are not available. In this case you will usually terminate the program, or a major subsection of it, and the exception is a good way of transmitting information about the problem. C++ Exceptions were designed for reporting errors which prevent the program continuing.

然后,异常的主要用途是:当外部前置条件或不变量(如内存或磁盘空间等足够资源)不可用时报告。在这种情况下,您通常会终止程序或程序的主要部分,而异常是传输有关问题的信息的好方法。c++异常用于报告错误,防止程序继续。

The exception handling model used in most modern languages including C++ is known to be broken. It is vastly too powerful. Theoreticians have now developed better models than the completely open "throw anything" and "maybe and maybe not catch it" model. In addition using type information to classify exceptions wasn't a very good idea.

在大多数现代语言中使用的异常处理模型(包括c++)是众所周知的。它太强大了。理论家们现在已经开发出比完全开放的“抛出任何东西”和“可能或可能不会抓住它”模型更好的模型。此外,使用类型信息对异常进行分类并不是一个好主意。

So the best thing you can do is throw exceptions sparingly, when there's an actual error, and when there's no other way to deal with it and catch exceptions as close to the throw point as possible.

所以你能做的最好的事情是,当有一个真正的错误时,当没有其他方法来处理它时,尽量少抛出异常,并尽可能地捕获异常。

#3


2  

Best read for this

最好的阅读这

Exception handling has been talked about a lot over the last decade and a half. However, despite a general consensus on how to properly handle exceptions, a divide on usage continues to exist. Improper exception handling is easy to spot, easy to avoid, and is a simple code (and developer) quality metric. I know absolute rules come off as close minded or exaggerated, but as a general rule you shouldn’t be using try/catch

在过去的15年中,异常处理已经被谈论了很多。然而,尽管在如何正确处理异常方面达成了普遍共识,但在使用方面仍存在分歧。不适当的异常处理很容易发现,很容易避免,并且是一个简单的代码(和开发人员)质量度量。我知道绝对的规则给人的感觉是很严密或者很夸张,但一般来说,你不应该使用try/catch

http://codebetter.com/karlseguin/2010/01/25/don-t-use-try-catch/

http://codebetter.com/karlseguin/2010/01/25/don-t-use-try-catch/

#4


1  

1.An exception check is included in the code when there is a possibility of getting an exception as a result or somewhere in between the problem.

1。当可能得到异常的结果或问题之间的某个地方时,代码中包含一个异常检查。

2.Use try-catch block with only those cases where it is required. Usage of each try-catch block add to an extra condition check that certainly reduces the optimization of the code.

2。只在需要的情况下使用try-catch块。每个try-catch块的使用都会添加一个额外的条件检查,这肯定会减少代码的优化。

3.I think _try_except is a valid variable name....

3所示。我认为_try_except是一个有效的变量名....

#5


1  

If your problem comes from your own bad code, it's better to use ASSERTs to guard against it. Exception handling is needed to identify problems that program cannot handle and tell them about the user, because user can handle them. But bugs in your program are not something the user can handle, so program crashing will tell not much

如果您的问题来自您自己的坏代码,那么最好使用assert来防止它。需要异常处理来识别程序无法处理的问题,并告诉它们关于用户的问题,因为用户可以处理它们。但是程序中的错误不是用户可以处理的,所以程序崩溃不会说明什么

I disagree with this aspect of the accepted answer. An assert is not hands-down better than throwing an exception. If exceptions were suitable only for run-time errors (or "external problems") , what is std::logic_error for?

我不同意这个公认答案的这方面。断言当然不如抛出异常好。如果异常只适用于运行时错误(或“外部问题”),那么std::logic_error用于什么?

A logic error is almost by definition the kind of condition that prevents a program from continuing. If the program is a logical construct, and a condition occurs outside the domain of that logic, how can it continue? Gather ye inputs while ye may, and throw an exception!

逻辑错误几乎可以定义为阻止程序继续运行的条件。如果程序是一个逻辑构造,并且条件发生在该逻辑的域之外,它如何继续?趁你们还可以的时候,聚集你们的意见,抛出一个例外!

It's not like there's not prior art. std::vector, to name but one, throws a logic error exception, namely std::out_of_range. If you use the standard library and don't have a top-level handler to catch standard exceptions -- if only to call what() and exit(3) -- then your programs are subject to abrupt silent, termination.

这并不是说没有现有技术。std:::vector,首先抛出一个逻辑错误异常,即std::out_of_range。如果您使用的是标准库,并且没有*处理程序来捕获标准异常(如果只调用what()和exit(3))),那么您的程序就会突然陷入静默,终止。

An assert macro is a much weaker guard. There is no recovery. Unless, that is, you're not running a debug build, in which case there's no execution. The assert macro belongs to an era when computation was 6 orders of magnitude slower than today. If you're going to the trouble to test for logic errors, but not to use that test when it counts, in production, you'd better have a lot of confidence in your code!

断言宏是一个弱得多的保护。没有复苏。除非,也就是说,您没有运行调试构建,在这种情况下没有执行。断言宏属于一个计算速度比今天慢6个数量级的时代。如果您在测试逻辑错误时遇到了麻烦,但是在产品中,您最好对您的代码充满信心!

The standard library provides for logic error exceptions, and employs them. They are there for a reason: because logic errors occur, and are exceptional. Just because C features assertions is no reason to rely on such a primitive (and, arguably, useless) mechanism, when an exception handles the job so much better.

标准库提供了逻辑错误异常,并使用它们。它们存在的原因是:因为逻辑错误会发生,而且是异常的。仅仅因为C特性断言没有理由依赖这样一种原始的(而且可以说是无用的)机制,当异常处理这个任务时就会更好。

#6


-1  

The basical difference is:

基本的区别是:

  1. one make error handling for you.
  2. 一个是错误处理。
  3. an one is you do your own.

    一个是你自己做。

    • For instance, you have an expression could make 0 divide error. Using try catch 1. will help you when error occurred. Or you need an if a==0 then.. in 2.

      例如,你有一个表达式可以产生0除错误。使用试着赶上1。将在发生错误时帮助您。或者你需要a= 0。在2。

    • If you dont try catch the exception I dont think its faster, its just simply bypass, if error occurred it will be threw to an outer handler.

      如果你不尝试捕捉异常,我不认为它更快,它只是简单的绕过,如果发生错误,它将被扔给外部处理器。

Handing yourself mean the problem not go further then have advantage in speed in many case, but not always.

把自己拱手相让意味着问题不会越走越远,在很多情况下都有速度优势,但并非总是如此。

Suggest: Just handling yourself when it simple and in logically case.

建议:在简单和合乎逻辑的情况下处理你自己。

#7


-5  

Many C/C++ purists discourage exceptions altogether. The main criticisms are:

许多C/ c++纯粹主义者完全不鼓励例外。最主要的批评是:

  1. It's slow - Of course it isn't really "slow". However, compared to pure c/c++, there is quite a bit of overhead.
  2. 它是慢的——当然它并不是真的“慢”。然而,与纯c/c++相比,它有相当大的开销。
  3. It introduces bugs - If you don't handle exceptions properly, you can miss the clean-up code in the function that throws the exception.
  4. 它引入了错误——如果您不正确地处理异常,您可能会错过抛出异常的函数中的清理代码。

Instead, check the return value / error code every time you call a function.

相反,每次调用函数时都要检查返回值/错误代码。

#1


66  

Here's quite comprehensive guide on exceptions that I think is a Must Read:

这里有一个关于例外的全面指南,我认为这是必须阅读的:

Exceptions and error handling - C++ FAQ or C++ FAQ lite

异常和错误处理- c++ FAQ或c++ FAQ lite

As a general rule of thumb, throw an exception when your program can identify an external problem that prevents execution. If you receive data from the server and that data is invalid, throw an exception. Out of disk space? Throw an exception. Cosmic rays prevent you from querying the database? Throw an exception. But if you get some invalid data from inside your very own program - don't throw an exception. If your problem comes from your own bad code, it's better to use ASSERTs to guard against it. Exception handling is needed to identify problems that program cannot handle and tell them about the user, because user can handle them. But bugs in your program are not something the user can handle, so program crashing will tell not much less than "Value of answer_to_life_and_universe_and_everything is not 42! This should never happen!!!!11" exception.

作为一般的经验法则,当您的程序可以识别阻止执行的外部问题时,抛出异常。如果您从服务器接收数据并且该数据无效,抛出异常。磁盘空间?抛出异常。宇宙射线阻止你查询数据库?抛出异常。但是,如果您从自己的程序中获得了一些无效的数据,请不要抛出异常。如果您的问题来自您自己的坏代码,那么最好使用assert来防止它。需要异常处理来识别程序无法处理的问题,并告诉它们关于用户的问题,因为用户可以处理它们。但是程序中的错误并不是用户可以处理的,所以程序崩溃将告诉你“答案是什么?”这应该不会发生! ! ! !11“例外。

Catch an exception where you can do something useful with it, like, display a message box. I prefer to catch an exception once inside a function that somehow handles user input. For example, user presses button "Annihilate all hunams", and inside annihilateAllHunamsClicked() function there's a try...catch block to say "I can't". Even though annihilation of hunamkind is a complex operation that requires calling dozens and dozens of functions, there is only one try...catch, because for a user it's an atomic operation - one button click. Exception checks in every function are redundant and ugly.

捕获一个异常,您可以使用它做一些有用的事情,比如显示消息框。我希望在处理用户输入的函数中捕获一次异常。例如,用户按下按钮“Annihilate all hunams”,在annihilateAllHunamsClicked()函数中有一个try…抓住block说“我不能”。尽管消灭hunamkind是一个复杂的操作,需要调用数十个函数,但只有一次尝试……catch,因为对于一个用户来说,它是一个原子操作-一个按钮点击。每个函数中的异常检查都是冗余和丑陋的。

Also, I can't recommend enough getting familiar with RAII - that is, to make sure that all data that is initialized is destroyed automatically. And that can be achieved by initializing as much as possible on stack, and when you need to initialize something on heap, use some kind of smart pointer. Everything initialized on the stack will be destroyed automatically when an exception is thrown. If you use C-style dumb pointers, you risk memory leak when an exception is thrown, because there is noone to clean them up upon exception (sure, you can use C-style pointers as members of your class, but make sure they are taken care of in destructor).

此外,我不建议您对RAII有足够的了解——也就是说,要确保所有初始化的数据都被自动销毁。这可以通过在堆栈上尽可能多地初始化来实现,当您需要在堆上初始化一些东西时,使用某种智能指针。当抛出异常时,堆栈上初始化的所有内容将被自动销毁。如果使用c风格的dumb指针,在抛出异常时可能会导致内存泄漏,因为没有人会在异常情况下清理它们(当然,您可以使用c风格的指针作为类的成员,但是要确保它们在销毁程序中得到了处理)。

#2


10  

Exceptions are useful in a variety of circumstances.

例外在各种情况下都是有用的。

First, there are some functions where the cost of calculating the pre-condition is so high it is better to just do the calculation and abort with an exception if it is found the pre-condition is not met. For example, you cannot invert a singular matrix, however to calculate if it is singular you calculate the determinant which is very expensive: it may have to be done inside the function anyhow, so just "have a try" at inverting the matrix and report an error if you can't by throwing an exception. This is basically an exception as negative pre-condition usage.

首先,在某些函数中,计算预条件的成本太高,如果发现不满足预条件,最好只进行计算并异常终止。例如,您不能逆奇异矩阵,但是计算如果是单数计算行列式很昂贵:这可能要做内部函数无论如何,只是“试一试”反相矩阵并报告一个错误如果你不能通过抛出异常。这基本上是一个作为消极前提使用的例外。

Then there are other cases where your code is already complex and passing error information up the call chain is difficult. This is partly because C and C++ have broken data structure models: there are other, better ways, but C++ doesn't support them (such as using monads in Haskell). This use is basically I couldn't be bothered to do it right so I'll throw an exception: its not the right way but it's practical.

还有一些情况,您的代码已经很复杂,并且很难将错误信息传递到调用链上。这部分是因为C和c++破坏了数据结构模型:还有其他更好的方法,但是c++不支持它们(比如在Haskell中使用monads)。这种用法基本上是我懒得去做的,所以我要抛出一个例外:它不是正确的方法,但它是实用的。

Then there is the main use of exceptions: to report when external pre-conditions or invariants, such as sufficient resources like memory or disk space, are not available. In this case you will usually terminate the program, or a major subsection of it, and the exception is a good way of transmitting information about the problem. C++ Exceptions were designed for reporting errors which prevent the program continuing.

然后,异常的主要用途是:当外部前置条件或不变量(如内存或磁盘空间等足够资源)不可用时报告。在这种情况下,您通常会终止程序或程序的主要部分,而异常是传输有关问题的信息的好方法。c++异常用于报告错误,防止程序继续。

The exception handling model used in most modern languages including C++ is known to be broken. It is vastly too powerful. Theoreticians have now developed better models than the completely open "throw anything" and "maybe and maybe not catch it" model. In addition using type information to classify exceptions wasn't a very good idea.

在大多数现代语言中使用的异常处理模型(包括c++)是众所周知的。它太强大了。理论家们现在已经开发出比完全开放的“抛出任何东西”和“可能或可能不会抓住它”模型更好的模型。此外,使用类型信息对异常进行分类并不是一个好主意。

So the best thing you can do is throw exceptions sparingly, when there's an actual error, and when there's no other way to deal with it and catch exceptions as close to the throw point as possible.

所以你能做的最好的事情是,当有一个真正的错误时,当没有其他方法来处理它时,尽量少抛出异常,并尽可能地捕获异常。

#3


2  

Best read for this

最好的阅读这

Exception handling has been talked about a lot over the last decade and a half. However, despite a general consensus on how to properly handle exceptions, a divide on usage continues to exist. Improper exception handling is easy to spot, easy to avoid, and is a simple code (and developer) quality metric. I know absolute rules come off as close minded or exaggerated, but as a general rule you shouldn’t be using try/catch

在过去的15年中,异常处理已经被谈论了很多。然而,尽管在如何正确处理异常方面达成了普遍共识,但在使用方面仍存在分歧。不适当的异常处理很容易发现,很容易避免,并且是一个简单的代码(和开发人员)质量度量。我知道绝对的规则给人的感觉是很严密或者很夸张,但一般来说,你不应该使用try/catch

http://codebetter.com/karlseguin/2010/01/25/don-t-use-try-catch/

http://codebetter.com/karlseguin/2010/01/25/don-t-use-try-catch/

#4


1  

1.An exception check is included in the code when there is a possibility of getting an exception as a result or somewhere in between the problem.

1。当可能得到异常的结果或问题之间的某个地方时,代码中包含一个异常检查。

2.Use try-catch block with only those cases where it is required. Usage of each try-catch block add to an extra condition check that certainly reduces the optimization of the code.

2。只在需要的情况下使用try-catch块。每个try-catch块的使用都会添加一个额外的条件检查,这肯定会减少代码的优化。

3.I think _try_except is a valid variable name....

3所示。我认为_try_except是一个有效的变量名....

#5


1  

If your problem comes from your own bad code, it's better to use ASSERTs to guard against it. Exception handling is needed to identify problems that program cannot handle and tell them about the user, because user can handle them. But bugs in your program are not something the user can handle, so program crashing will tell not much

如果您的问题来自您自己的坏代码,那么最好使用assert来防止它。需要异常处理来识别程序无法处理的问题,并告诉它们关于用户的问题,因为用户可以处理它们。但是程序中的错误不是用户可以处理的,所以程序崩溃不会说明什么

I disagree with this aspect of the accepted answer. An assert is not hands-down better than throwing an exception. If exceptions were suitable only for run-time errors (or "external problems") , what is std::logic_error for?

我不同意这个公认答案的这方面。断言当然不如抛出异常好。如果异常只适用于运行时错误(或“外部问题”),那么std::logic_error用于什么?

A logic error is almost by definition the kind of condition that prevents a program from continuing. If the program is a logical construct, and a condition occurs outside the domain of that logic, how can it continue? Gather ye inputs while ye may, and throw an exception!

逻辑错误几乎可以定义为阻止程序继续运行的条件。如果程序是一个逻辑构造,并且条件发生在该逻辑的域之外,它如何继续?趁你们还可以的时候,聚集你们的意见,抛出一个例外!

It's not like there's not prior art. std::vector, to name but one, throws a logic error exception, namely std::out_of_range. If you use the standard library and don't have a top-level handler to catch standard exceptions -- if only to call what() and exit(3) -- then your programs are subject to abrupt silent, termination.

这并不是说没有现有技术。std:::vector,首先抛出一个逻辑错误异常,即std::out_of_range。如果您使用的是标准库,并且没有*处理程序来捕获标准异常(如果只调用what()和exit(3))),那么您的程序就会突然陷入静默,终止。

An assert macro is a much weaker guard. There is no recovery. Unless, that is, you're not running a debug build, in which case there's no execution. The assert macro belongs to an era when computation was 6 orders of magnitude slower than today. If you're going to the trouble to test for logic errors, but not to use that test when it counts, in production, you'd better have a lot of confidence in your code!

断言宏是一个弱得多的保护。没有复苏。除非,也就是说,您没有运行调试构建,在这种情况下没有执行。断言宏属于一个计算速度比今天慢6个数量级的时代。如果您在测试逻辑错误时遇到了麻烦,但是在产品中,您最好对您的代码充满信心!

The standard library provides for logic error exceptions, and employs them. They are there for a reason: because logic errors occur, and are exceptional. Just because C features assertions is no reason to rely on such a primitive (and, arguably, useless) mechanism, when an exception handles the job so much better.

标准库提供了逻辑错误异常,并使用它们。它们存在的原因是:因为逻辑错误会发生,而且是异常的。仅仅因为C特性断言没有理由依赖这样一种原始的(而且可以说是无用的)机制,当异常处理这个任务时就会更好。

#6


-1  

The basical difference is:

基本的区别是:

  1. one make error handling for you.
  2. 一个是错误处理。
  3. an one is you do your own.

    一个是你自己做。

    • For instance, you have an expression could make 0 divide error. Using try catch 1. will help you when error occurred. Or you need an if a==0 then.. in 2.

      例如,你有一个表达式可以产生0除错误。使用试着赶上1。将在发生错误时帮助您。或者你需要a= 0。在2。

    • If you dont try catch the exception I dont think its faster, its just simply bypass, if error occurred it will be threw to an outer handler.

      如果你不尝试捕捉异常,我不认为它更快,它只是简单的绕过,如果发生错误,它将被扔给外部处理器。

Handing yourself mean the problem not go further then have advantage in speed in many case, but not always.

把自己拱手相让意味着问题不会越走越远,在很多情况下都有速度优势,但并非总是如此。

Suggest: Just handling yourself when it simple and in logically case.

建议:在简单和合乎逻辑的情况下处理你自己。

#7


-5  

Many C/C++ purists discourage exceptions altogether. The main criticisms are:

许多C/ c++纯粹主义者完全不鼓励例外。最主要的批评是:

  1. It's slow - Of course it isn't really "slow". However, compared to pure c/c++, there is quite a bit of overhead.
  2. 它是慢的——当然它并不是真的“慢”。然而,与纯c/c++相比,它有相当大的开销。
  3. It introduces bugs - If you don't handle exceptions properly, you can miss the clean-up code in the function that throws the exception.
  4. 它引入了错误——如果您不正确地处理异常,您可能会错过抛出异常的函数中的清理代码。

Instead, check the return value / error code every time you call a function.

相反,每次调用函数时都要检查返回值/错误代码。