Exception Handling
In C++, Exception
Handling is a mechanism to handle runtime errors in a structured way, so that
the normal flow of the program is not terminated abruptly.
Exception
An exception
is an abnormal condition or error that occurs during program execution.
Common
Runtime Errors
- Division by zero
- Array index out of bounds
- File not found
- Memory allocation failure
- Invalid user input
Need
Without
Exception Handling
- The program crashes suddenly
- Difficult to debug
- Error-handling code mixes with
normal logic
With
Exception Handling
✔
Separate error handling code
✔
Program continues execution safely
✔
Easy debugging and maintenance
✔
Improves program reliability
Keywords Used in Exception Handling
|
Keyword |
Purpose |
|
try |
Code that may cause an exception |
|
throw |
Used to generate an exception |
|
catch |
Handles the exception |
4. Basic
Structure of Exception Handling
try {
// Code that may cause an exception
throw exception;
}
catch
(exception_type e) {
// Handling code
}
Working Principle (Step-by-Step)
- Code inside the try block executes
- If an error occurs, → throw
statement executes
- Control jumps to the matching catch
block
- The exception is handled
- The program continues after the catch
Simple Practical Example
Example: Division by Zero
#include
<iostream>
using
namespace std;
int main()
{
int a, b;
cout << "Enter two numbers:
";
cin >> a >> b;
try {
if (b == 0)
throw b;
cout << "Result = " << a
/ b;
}
catch (int x) {
cout << "\nError: Division
by zero is not allowed";
}
return 0;
}
Output
Enter two
numbers: 10 0
Error:
Division by zero is not allowed
Multiple Catch Blocks
Used when different
types of exceptions may occur.
Syntax
try {
// code
}
catch (int
e) {
}
catch
(float e) {
}
catch
(...) {
}
Example
: Multiple Catch
#include
<iostream>
using
namespace std;
int main()
{
try {
throw 10.5;
}
catch (int e) {
cout << "Integer
Exception";
}
catch (float e) {
cout << "Float
Exception";
}
catch (...) {
cout << "Unknown
Exception";
}
return 0;
}
Catch All Exception (catch(...))
- Handles any type of exception
- Must be written last
catch
(...) {
cout << "Some error
occurred";
}
Throwing User-Defined Exceptions
Example
: Age Validation
#include
<iostream>
using
namespace std;
int main()
{
int age;
cout << "Enter age: ";
cin >> age;
try {
if (age < 18)
throw age;
cout << "Eligible for
voting";
}
catch (int a) {
cout << "Not eligible for
voting";
}
return 0;
}
Exception Handling with Functions
Example
: Function Throwing Exception
#include
<iostream>
using
namespace std;
void
check(int x) {
if (x < 0)
throw x;
cout << "Valid Number";
}
int main()
{
try {
check(-5);
}
catch (int e) {
cout << "Negative number
exception";
}
return 0;
}
Standard Exception Classes
C++
provides built-in exception classes in <exception> header.
|
Class |
Description |
|
exception |
Base class |
|
bad_alloc |
Memory allocation failure |
|
bad_cast |
Invalid type casting |
Summary
- try must be followed by at
least one catch
- catch(...) must be last
- Multiple throw statements are
allowed
- Exception type must match the catch parameter
- Exception handling works at runtime
Advantages of Exception Handling
Clean code
structure
Better error management
The program does not crash
Useful for large applications
Limitations
Slight
performance overhead
Not suitable for simple programs
========================================================================
0 Comments