PPT12 Exception Handling
PPT12 Exception Handling
Exception Handling
1
What is Exception Handling?
• Without proper error handling, programs can terminate abruptly, leading to poor user experiences and
system instability.
• Exception is a run-time error, such as divide by zero, array out of bounds, file not found etc.
• It helps to continue the normal flow of the application even in the case of runtime errors
• It makes the code simpler, cleaner, and less likely to miss errors
Exception Handling Mechanism
int main() {
int numerator = 10, denominator = 0;
#include <iostream>
using namespace std; try {
if (denominator == 0) {
class DivisionByZero { DivisionByZero ex;
public: throw ex; // Throw an object of the class
void errorMessage() { }
cout << "Error: Division by zero!" << endl; cout << "Result: " << numerator / denominator << endl;
} }
}; catch (DivisionByZero ex) {
ex.errorMessage();
}
return 0;
}
Catching All Exceptions (catch(...))
C++ provides a special syntax to catch any type of exception. The catch(...) block acts as a catch-all handler and
can be used when the type of exception is unknown.
int main() {
#include <iostream>
using namespace std; try {
throw 10;
int main() {
}
try {
throw 10; catch (char *e) { cout << "Caught " << e; }
} catch (...) { cout << "Default Exception\n"; }
catch (...) { // Catch any type of exception
return 0;
cout << "Caught an unknown exception!" << endl;
} }
return 0;
}
// What will be the output?
int main() {
try {
throw "10";
}
catch (const char *e) { cout << "Caught " << e; }
catch (...) { cout << "Default Exception\n"; }
Output
Caught 10
Throwing Exceptions from Functions
#include <iostream>
using namespace std;
int main() {
int x = 10, y = 0;
try {
cout << "Result: " << divide(x, y) << endl;
}
catch (const char* e) {
cout << "Error: " << e << endl;
}
return 0;
}
Multiple catch is possible
try {
//some code
throw exception;
}
catch(specific type exception) {//some code }
…
catch(generic type exception) { //some code }
Catch blocks must be ordered so that most specific exceptions get caught before generic exceptions.
Exception Specification
• C++ provides a mechanism to ensure that a given function is limited to throw only
a specified list of exceptions.
std::exception
std::logic_error std::runtime_error
+-- std::invalid_argument +-- std::range_error
+-- std::domain_error +-- std::overflow_error
+-- std::underflow_error
+-- std::length_error
+-- std::out_of_range
std::exception
Base Class for All Exceptions:-
std::exception is the base class for all standard exceptions. It
provides the virtual member function what() that returns an error
message.
int main() {
try {
throw std::exception();
}
catch (const std::exception& e) {
std::exception::what() returns a
cout << "Caught exception: " << e.what() << endl; generic message "std::exception"
}
return 0;
}
std::logic_error
• Std::logic_error is a derived class of std::exception used for errors in
the logic of the program (e.g., domain errors, invalid arguments).
• These errors represent conditions that could be prevented with
proper input validation.
Derived Classes: