Sutter/Alexandrescu explanation of Exceptions
-
Sutter and Alexandrescu have a section on proper use of C++ exceptions that strongly encourages the use of C++ exceptions. I am confused by the last item, number 75, where they suggest avoiding exception specifications. 1) If you do not throw, then why bother trying to catch, which is what the previous items advocate? 2) If exception specifications are bad why does C++ support them? Can someone please explain what I am missing? Thanks
-
Sutter and Alexandrescu have a section on proper use of C++ exceptions that strongly encourages the use of C++ exceptions. I am confused by the last item, number 75, where they suggest avoiding exception specifications. 1) If you do not throw, then why bother trying to catch, which is what the previous items advocate? 2) If exception specifications are bad why does C++ support them? Can someone please explain what I am missing? Thanks
Member 2603772 wrote:
If you do not throw, then why bother trying to catch, which is what the previous items advocate?
Not using exception specifications doesn't mean you are guaranteeing that your code doesn't throw an exception - it's the reverse of that - you are making no guarantees about whether your code throws exceptions or not. If a piece of code you're calling has no exception specification, assume that it may throw *any* exception. Only handle exceptions if there's some action you can perform that fixes what caused the exception. Any other exception should be allowed to terminate the application - if you don't know how to fix what caused the exception, then you've got problems.
Member 2603772 wrote:
If exception specifications are bad why does C++ support them?
They're not bad per se. The point is that C++ doesn't enforce them or (really) take any notice of them. That means that compilers won't verify that they're correct, so they're liable (like code comments) to become inaccurate as you change the code they describe, because you probably won't remember to change the exception specification. This article[^] by Herb Sutter may help to make his view on exception specifications clearer?
-
Sutter and Alexandrescu have a section on proper use of C++ exceptions that strongly encourages the use of C++ exceptions. I am confused by the last item, number 75, where they suggest avoiding exception specifications. 1) If you do not throw, then why bother trying to catch, which is what the previous items advocate? 2) If exception specifications are bad why does C++ support them? Can someone please explain what I am missing? Thanks
Maybe this article[^] helps you understand the exception specifications and why they are not a good idea.