#include <iostream>
using namespace std;
int divide(int a, int b) {
if (b == 0) {
throw "Division by zero error!";
}
return a / b;
}
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
try {
int result = divide(num1, num2);
cout << "Result: " << result << endl;
} catch (const char* error) {
cout << "Error caught: " << error << endl;
}
cout << "Program continues after exception handling." << endl;
return 0;
}Output
Enter two numbers: 10 0 Error caught: Division by zero error! Program continues after exception handling.
This program teaches you how to use Basic try-catch Blocks in C++. Exception handling allows programs to gracefully handle errors and unexpected situations without crashing. The try-catch mechanism is essential for robust, production-quality code.
1. What This Program Does
The program demonstrates basic exception handling:
- Throwing exceptions using throw statement
- Catching exceptions using try-catch blocks
- Handling division by zero error
- Continuing program execution after exception
Exception handling prevents crashes and enables graceful error management.
2. Header Files Used
- #include <iostream>
- Provides cout and cin for input/output operations.
3. Understanding Exception Handling
Exception Concept:
- Errors or unexpected situations
- Thrown when problem occurs
- Caught and handled gracefully
- Prevents program crashes
Key Components:
- try: code that might throw
- throw: raises exception
- catch: handles exception
- Program continues after catch
4. Throwing Exceptions
Using throw:
if (b == 0) { throw "Division by zero error!"; }
How it works:
- throw statement raises exception
- Can throw any type (string, int, object)
- Control immediately jumps to catch
- Code after throw not executed
5. Try Block
Wrapping Code:
try { int result = divide(num1, num2); cout << "Result: " << result << endl; }
How it works:
- Code that might throw goes in try
- If exception thrown, jumps to catch
- If no exception, continues normally
- Can have multiple statements
6. Catch Block
Handling Exception:
catch (const char* error) { cout << "Error caught: " << error << endl; }
How it works:
- Catches specific exception type
- Executes when exception thrown
- Receives exception value
- Program continues after catch
7. When to Use Exception Handling
Best For:
- Error conditions
- Invalid input
- Resource failures
- Unexpected situations
- Robust error handling
Example Scenarios:
- Division by zero
- File not found
- Invalid user input
- Network errors
- Memory allocation failures
8. Important Considerations
Exception Types:
- Can throw any type
- Strings, integers, objects
- Standard exception classes
- Custom exception classes
Program Flow:
- Exception jumps to catch
- Code after throw not executed
- Program continues after catch
- Prevents crashes
Error Messages:
- Provide clear error messages
- Help with debugging
- Inform users of problems
- Guide error resolution
9. return 0;
This ends the program successfully.
Summary
- Exception handling: use try-catch blocks to handle errors gracefully.
- throw: raises exception when error occurs.
- catch: handles exception, receives exception value.
- Program continues after catch block, prevents crashes.
- Understanding exception handling enables robust error management.
- Essential for production-quality code and graceful error handling.
This program is fundamental for learning error handling, understanding exception mechanisms, and preparing for robust program development in C++ programs.