-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionDemoDivision.cpp
More file actions
37 lines (31 loc) · 1.16 KB
/
ExceptionDemoDivision.cpp
File metadata and controls
37 lines (31 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*Write a program to demonstrate the catching of all exceptions.*/
#include <iostream>
using namespace std;
int main() {
try {
// Some code that may throw exceptions
int numerator, denominator, result;
// Input numerator and denominator from the user
cout << "Enter the numerator: ";
cin >> numerator;
cout << "Enter the denominator: ";
cin >> denominator;
// Check if the denominator is zero, and throw an exception if it is
if (denominator == 0) {
throw runtime_error("Division by zero is not allowed.");
}
// Perform the division and display the result
result = numerator / denominator;
cout << "Result: " << result << endl;
}
catch (exception& ex) {
// Catch block for exceptions derived from std::exception
cerr << "Caught an exception: " << ex.what() << endl;
}
catch (...) {
// Catch-all block for catching any other uncaught exceptions
cerr << "Caught an unknown exception." << endl;
}
cout << "Program continues after exception handling." << endl;
return 0;
}