Why exceptions rarely used in C ++

I have been programming in C ++ for 5 years, why have I never seen the exceptions used instead of examples for examples?

+7
source share
11 answers

Observational bias when working here.

A significant part of C ++ code is intended for system programming and embedded systems. (In the end, C ++ is just one of many programming options for applications, and many of the alternatives have more favorable RAD environments, but usually systems work at the highest level for which the compiler is available, with a large scope.) And most embedded systems, as well as a significant part of system development work, have limitations that exclude exceptions.

If, due to your attention, you are trying to find such code, it is quite possible that the C ++ code you saw does not use exceptions.

This does not mean that C ++ code is not used with exceptions - there are many. It may simply not appear in code designed to solve the problems you are interested in.

+15
source

Exceptions are a rather late addition to the language, so I believe that many C ++ developers have never learned how to use them correctly and can feel more at ease using traditional C style error handling methods.

I personally believe that they are indispensable in handling designer crashes (this is the main reason they were added in C ++), but they also require the correct use of other advanced (YMMV) methods, such as RAII or smart pointers, to avoid the resource leaks.

+10
source

I have been programming in C ++ for 5 years, why have I never seen the exceptions used instead of examples for examples?

I'm very curious. I am very curious about this since 1996. I used to think that in 1996, C ++ Exception Handling revolutionized the way I write software. I remember that I read about handling C ++ Exception, and I immediately understood the consequences. After a few minutes, I tested what happens if an exception is thrown from the constructor. Compilers for UNIX were not ready for C ++ Exception Handling before g ++ 3.0, I think (was that?). Destructors were called for undesigned memory locations (on the stack) (in the event of an exception being thrown). Destructors were not called for successfully constructed objects (on the stack) (in case of any exception). delete was not called if the object created with new threw an exception from the constructor. Compilers for Windows and OS / 2 were ready in 1996/1997. They worked. I remember Borland C ++ for OS / 2 and IBM CSet2 and Windows Visual C ++.

Finally, there was a method of interrupting the construction of an object. Finally, one could select an object inside the constructor AND rely on the successful construction of this object in some other constructor. Somehow I found out about all the rules. Not from books! Years later, books appeared claiming that C ++ Exception Handling is a good way to catch the "out-of-bounds" error or other problems for which I never stopped using assert. Finally, there was an easy way to provide the caller with complex information about some error without relying on stderr. Finally, there was no need to debug some complex software tools to find out what didn't work out.

I can't take seriously people who don't use C ++ Exception Handling. Unable to check every erroneous call. It is not possible to achieve the same level of software quality without using C ++ Exception Handling. Why are such people still hired? Why there are still platforms that do not provide C ++ Exception Handling. I would never have thought of writing software for such a platform, in the same way I would have refused to write a complex application in assembly code.

+7
source

Curious. I regularly work in C ++, and it has been at least ten years since I saw C ++ code that does not use exceptions. Every time you have to propagate the error to a significant number of stack frames, you use an exception.

+6
source

Several reasons come to mind:

  • Exceptions should not be very noticeable, as they are designed to be thrown deep into the bowels of the library and caught somewhere high in the call stack (even before main() ).
  • They are designed to signal exceptional (i.e. rare and unexpected) malfunctions. For example, a failure to open a file is not particularly exceptional. Thus, by default, the iostream library does not throw an exception when it fails to open the file.
  • Exceptions are very expensive to throw, which contributes to the project’s intentions.
  • C ++ libraries that throw exceptions do not easily interact with C programs.
+4
source

I could say the same thing, and it would not be biased if I determined that this was true for the world of Microsoft and RAD.

I think this is because today you are not using C ++ programs as such, but rather a mixture of a high-level language using C ++ libraries. And often you have a managed unmanaged border.

Throw and catch the exception across this border, it's like lighting a firework in your ass :) - [read memory leak or worse]

Also, if you use COM objects, you need to use COM exceptions, so using a standard C ++ exception should be inside the often small library. In small libraries you do not need to use exceptions.

+1
source

Because there is a huge discrepancy between the "real" code and the "tutorial" code.

I work for a “big” software company and I can honestly tell you that the material you see in the production process makes up almost 0% of the good practice that you read about in good books.

As an example, take Scott Myers' Effective C ++ Book. There should be a copy on every desk of a software engineer, from the east coast, to the west.

+1
source

Exceptions are too advanced for beginners, so they are not always shown in examples, especially in books.

+1
source

Several reasons come to mind:

  • Exceptions should not be very noticeable, as they are intended to be thrown deep in the bowels of the library and caught somewhere high in the call stack (even higher than main ()).

I do not know what you mean: "Exceptions should not be very noticeable." But I agree that capture blocks should be rare - and they are usually found mostly.

  • They are designed to signal exceptional (that is, rare and unexpected) faults. For example, a failure to open a file is not particularly exceptional. Thus, by default, the iostream library does not throw an exception when it fails to open the file.

that the iostream library does not throw exceptions makes it impractical. Hide errors from the caller! It is so similar to C.

  • Exceptions are very expensive to throw, which contributes to compliance with the intent of the project.

Typically, exceptions are for system call failures. Since writing to a file or opening a file is actually small, anyway, exceptions will be expensive. Also, checking for success is more expensive than using C ++ Exception Handling. Usually you do not need to create a try-catch block inside the temporary critical part of the code.

  • C ++ libraries that throw exceptions do not easily interact with C programs.

What is C? Oh yes, I remember that I refused 1996. Remember Turbo Pascal?

0
source

I am talking about desktop applications for Windows. In my observation (YMMV, too), an earlier phase of development is likely before the first releases. Many developers do not think about exceptions early on. This is why there are several exception handling codes, but if you already had 2 or 3 releases or at your service stage, exceptions are addressed due to different deployment environments through client error reporting.

0
source

Exceptions are not used in the examples, because this rarely affects many issues or something that you want to know first.

0
source

All Articles