Does exception handling require object-oriented programming? Does exception handling require object-oriented programming? c c

Does exception handling require object-oriented programming?


C lacks nothing in machine code, and exception handling was and is commonplace in C with setjmp and longjmp.

The reason for the complete lack of a language-level feature in purely procedural languages is that exception handling is identical to setjmp when no destructors need to be called. Exception handling has been around before in exotic languages, but never caught on because it was purely syntactic sugar. However, once destructors entered the scene and stack unwinding became necessary, language-level support became necessary and exception handling was widely implemented as part of the language.


Does exception handling require object-oriented programming?

No. The two are completely separate. One can have OO languages that do not have exception handling as a control flow primitive, and one can have exception handling in non-OO languages.

Object-oriented programming, as Wikipedia helpfully points out, is a style of programming that emphasizes the value of abstraction, encapsulation, messaging, modularity, polymorphism, and inheritance in order to achieve low-cost code re-use and effective management of complex software projects implemented by large teams.

You don't see "loops" or "if statements" or "goto" or "try-catch-finally-throw" on that list because control flow primitives have nothing whatsoever to do with abstraction, encapsulation, messaging, modularity, polymorphism or inheritance being used to achieve low cost code reuse or effective management of complex software projects by large teams.

What is that C lacks that to say, C++, in machine code that makes exceptions work?

It is certainly the case that modern hardware is designed with exception handling as a control flow primitive in mind. C was designed long before that modern hardware existed, which would make it somewhat more difficult to implement exception handling in C that runs efficiently on all the hardware that C runs on.

But that said, there's nothing stopping you or anyone else from designing a new version of C that has exception handling as a control flow primitive, but without all the other features of C++.

If you're interested in the subject of how to add exception handling to non-OO languages that support continuations, see my article on the subject that sketches out the idea:

http://blogs.msdn.com/b/ericlippert/archive/2010/10/22/continuation-passing-style-revisited-part-two-handwaving-about-control-flow.aspx


With that said, is there something about the object oriented nature of a language that allows exception handling, or was exception handling added as a feature as object oriented languages really started becoming a commonplace?

I first got to know exceptions when I had to learn Ada (studying CS) in the early 90s. IIRC, Ada had a special type Exception. It was, back then, not an object-oriented language. (Ada95 added some OO concepts.) However, I agree that stack unwinding (i.e. the complete automatic cleanup of allocated resources) is an important trait for the success of exception handling. Combining destructors with exception handling is an important point for the success of exceptions in C++.

I also seem to remember Stroustrup mentioning Ada as a major influence for exception handling in C++.