0% found this document useful (0 votes)
11 views5 pages

Exceptions and Errors

The document explains the differences between exceptions and errors, highlighting that exceptions can be handled programmatically while errors are severe and unmanageable. It discusses the advantages of using try-catch over if-else for error checking, emphasizing separation of concerns and efficiency. Additionally, it illustrates how throw can be utilized in constructors to signal failure during object creation, with examples provided for each concept.

Uploaded by

khanyyasir40
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Exceptions and Errors

The document explains the differences between exceptions and errors, highlighting that exceptions can be handled programmatically while errors are severe and unmanageable. It discusses the advantages of using try-catch over if-else for error checking, emphasizing separation of concerns and efficiency. Additionally, it illustrates how throw can be utilized in constructors to signal failure during object creation, with examples provided for each concept.

Uploaded by

khanyyasir40
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Difference Between Exceptions and Errors

Explanation:

- Exceptions are conditions that can be handled in the program using constructs like try, catch, and

throw.

- Errors are severe conditions often caused by hardware or memory issues and cannot be handled

programmatically.

Example Code:

#include <iostream>

using namespace std;

void exceptionExample() {

try {

int a = 10, b = 0;

if (b == 0) {

throw runtime_error("Division by zero exception!");

cout << "Result: " << a / b << endl;

} catch (const runtime_error& e) {

cout << "Exception caught: " << e.what() << endl;

void errorExample() {

// Simulating an error (stack overflow or memory exhaustion cannot be caught)


cout << "Errors like stack overflow cannot be handled programmatically." << endl;

int main() {

exceptionExample();

errorExample();

return 0;

2. Why is try-catch Better Than Using if-else for Error Checking?

Explanation:

- Separation of Concerns: Keeps error handling separate from business logic.

- Efficiency: Reduces repetitive condition checks.

- Scalability: Handles multiple exceptions more efficiently.

Code Comparison:

Using if-else:

#include <iostream>

using namespace std;

void divide(int a, int b) {

if (b == 0) {

cout << "Error: Division by zero!" << endl;

} else {

cout << "Result: " << a / b << endl;


}

int main() {

divide(10, 0);

return 0;

Using try-catch:

#include <iostream>

using namespace std;

void divide(int a, int b) {

try {

if (b == 0) {

throw runtime_error("Division by zero exception!");

cout << "Result: " << a / b << endl;

} catch (const runtime_error& e) {

cout << "Exception caught: " << e.what() << endl;

int main() {

divide(10, 0);

return 0;

}
3. Can throw Be Used in Constructors? If Yes, How Would You Handle It?

Explanation:

- throw can be used in constructors to signal that object creation failed.

- Handle such exceptions using a try-catch block where the object is created.

Example Code:

#include <iostream>

#include <string>

using namespace std;

class MyClass {

public:

MyClass(string name) {

if (name.empty()) {

throw runtime_error("Name cannot be empty!");

cout << "Object created with name: " << name << endl;

};

int main() {

try {

MyClass obj(""); // This will throw an exception

} catch (const runtime_error& e) {

cout << "Exception caught: " << e.what() << endl;


}

return 0;

Visit Website

For more resources, visit: Mehtab Alam's Website: https://mehtab.netlify.app/

You might also like