
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Throwing Exceptions from C++ Constructors
In C++, constructer is a special member function used to initialize objects. However, the constructor may encounter a scenario where it can't successfully initialize an object.
For example, failing to allocate memory or open a necessary file. In such a scenario, throwing an exception from a constructor is the appropriate way to signal failure (where the constructor is unable to perfectly initialize an object).
Why Throw Exception from Constructors?
If it fails to create an object. It's important to prevent the use of partially constructed objects. Throwing an exception allows us to:
- Signal an error clearly and immediately.
- Ensure the caller does not proceed with an invalid object.
- Avoid undefined behavior or complicated error-checking logic.
Syntax
Following is the syntax of throwing exceptions from a constructor ?
#include <iostream> #include <stdexcept> class FileHandler { public: FileHandler(const std::string& fileName) { // Simulating a failure (e.g., file not found) if (fileName.empty()) { throw std::invalid_argument("File name cannot be empty"); } // Pretend file is opened here std::cout << "File opened successfully: " << fileName << std::endl; } };
We can use the above exception code like:
int main() { try { FileHandler fh(""); // This will throw } catch (const std::exception& e) { std::cerr << "Constructor failed: " << e.what() << std::endl; } return 0; }
Example
In this example, we demonstrate how exceptions thrown from a constructor behave in C++ and how destructors are called depending on object construction status.
#include <iostream> using namespace std; class Sample1 { public: Sample1() { cout << "Construct an Object of sample1" << endl; } ~Sample1() { cout << "Destruct an Object of sample1" << endl; } }; class Sample2 { public: Sample2() { int i = 7; cout << "Construct an Object of sample2" << endl; throw i; } ~Sample2() { cout << "Destruct an Object of sample2" << endl; } }; int main() { try { Sample1 s1; Sample2 s2; } catch(int i) { cout << i << endl; } }
Following is the output of the above code ?
Construct an Object of sample1 Construct an Object of sample2 Destruct an Object of sample1 Caught 7
Example
In the following example, constructor throw, destructor behaviour ?
#include <iostream> using namespace std; class A { public: A() { cout << "Constructor of A" << endl; } ~A() { cout << "Destructor of A" << endl; } }; class B { public: B() { cout << "Constructor of B" << endl; throw "Error in B's constructor!"; } ~B() { cout << "Destructor of B" << endl; } }; int main() { try { A a; B b; // Exception thrown here } catch (const char* msg) { cout << "Caught exception: " << msg << endl; } return 0; }
Following is the output of the above code ?
Constructor of A Constructor of B Destructor of A Caught exception: Error in B's constructor!