Programming karte waqt kabhi-kabhi aisi situations aa jaati hain jo hum expect nahi karte — jaise divide by zero, file not found, memory overflow, etc. In problems ko handle karne ke liye C++ mein aata hai Exception Handling.
Exception Handling ka use program crash hone se bachane ke liye hota hai. Ye hume error ko gracefully handle karne ka tareeka deta hai using:
try
catch
throw
1. Basic try-catch-throw Syntax
try {
// Code jisme error aane ka chance ho
}
catch (exceptionType e) {
// Error handle karne wala block
}
Example:
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 0;
try {
if (b == 0)
throw "Divide by zero error!";
cout << "Result: " << a / b << endl;
}
catch (const char* msg) {
cout << "Caught Exception: " << msg << endl;
}
return 0;
}
throw
se error uthaya jaata hai, catch
block us error ko handle karta hai.
2. Multiple catch blocks
Aap multiple catch
blocks likh sakte ho, alag-alag types ke exceptions ke liye:
try {
throw 10; // integer exception
}
catch (int x) {
cout << "Integer Exception: " << x << endl;
}
catch (const char* str) {
cout << "String Exception: " << str << endl;
}
3. Catch-all block (...
)
Kabhi-kabhi aapko pata nahi hota ki kaunsi type ka exception aayega. Aise mein use hota hai:
catch (...) {
cout << "Problem Occurs! Exception caught." << endl;
}
4. Custom Exception Class
Aap khud ke exceptions bhi bana sakte ho using classes.
Example:
#include <iostream>
using namespace std;
class MyException {
public:
const char* what() {
return "Custom Exception Occurred!";
}
};
int main() {
try {
throw MyException();
}
catch (MyException e) {
cout << e.what() << endl;
}
return 0;
}
Tips:
what()
function usually return karta hai error ka message.- You can also inherit from
std::exception
for better practice.
5. Standard Exceptions in C++
C++ already kuch standard exception classes provide karta hai:
std::exception
: Base class
std::runtime_error
: Run-time errors
std::out_of_range
: Out of range containers
std::bad_alloc
: Memory allocation failure
std::logic_error
: Logical mistakes in code
Real-World Example: ATM Withdrawal
#include <iostream>
using namespace std;
class InsufficientFunds {
public:
const char* what() {
return "Insufficient Balance!";
}
};
int main() {
int balance = 1000, withdraw;
cout << "Enter amount to withdraw: ";
cin >> withdraw;
try {
if (withdraw > balance)
throw InsufficientFunds();
balance -= withdraw;
cout << "Transaction successful. Remaining: " << balance << endl;
}
catch (InsufficientFunds e) {
cout << "Error: " << e.what() << endl;
}
return 0;
}
Prectice Questions
Question 1: Division with Exception Handling
Problem:
Write a C++ program that takes two integers as input from the user and performs division. If the divisor is zero, throw and catch an exception that displays "Cannot divide by zero."
Usethrow
to raise the error andcatch
to handle it gracefully.
Question 2: Custom Exception – Bank Withdrawal
Problem:
Create a class LowBalanceException
. Write a program that simulates a bank account withdrawal. If the withdrawal amount is more than the available balance, throw LowBalanceException
with a proper error message.
Make a custom exception class and override a method like what()
to return a custom message.
Comments