Exception Handling in C++

Rishabh Agarwal
5 min readApr 29, 2023
Photo by David Pupăză on Unsplash

Exception handling involves predicting potential issues in a program and writing code to handle such faults or exceptions. In this article, we will discuss exception handling in C++. Unlike C, which lacks support for exception handling, C++ offers robust exception handling built into the programming language.

Design Philosophy

Exceptions in C++ were built keeping in mind some of the design considerations. Let us take a look at some of them and try to understand them.

(1) Code for error handling should be separated from the normal code — It should not happen that error handling mechanism is implemented in such a way that it is hard to tell what part of the code performs the actual functionality and what parts of the code are there to handle errors.

(2) Error handling should be a part of language itself which is unlike C where error handling is introduced as a part of libraries.

(3) It should support destruction of dynamic memory in case of exceptions.

(4) Exceptions can be propagated through any levels of stack.

(5) A single handler should be able to handle various kinds of exceptions.

Exception Handling in C++

--

--

Rishabh Agarwal

Software Developer 2 @ Schrödinger | IIT CSE Graduate - Writes about Software Engineering!