-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathException_handling.cpp
77 lines (64 loc) · 1.62 KB
/
Exception_handling.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Exception Handling in C++
// Exception Handling in C++ is a powerful mechanism that handles runtime errors, such as divide by zero, out of bound array access, etc.
// Example - 1
// #include <bits/stdc++.h>
// using namespace std;
// int main()
// {
// int a = 10, b = 0, c;
// try
// {
// if (b == 0)
// throw 1;
// c = a / b;
// cout << c << endl;
// }
// catch (int e)
// {
// cout << "Division by zero not allowed" << endl;
// cout << e << endl;
// }
// return 0;
// }
// Output:
// Division by zero not allowed
// 1
// Example - 2
// #include <iostream>
// using namespace std;
// int main()
// {
// int c; // Declare 'c' before using it
// try
// {
// cout << c << endl; // 'c' is declared, but its value is uninitialized
// throw 1; // Throw an integer exception
// cout << c << endl; // This line will not be executed
// }
// catch (int e)
// {
// cout << "Caught an exception with value: " << e << endl;
// }
// return 0;
// }
// Example - 3
#include <iostream>
#include <string>
using namespace std;
int main()
{
int c; // Declare 'c' before using it
cout<<"Enter the value of C\n";
cin>>c;
try
{
cout << c << endl; // 'c' is declared, but its value is uninitialized
throw "\nThis is not valid Integer Value\n"; // Throw an integer exception
cout << c << endl; // This line will not be executed
}
catch (const char* e)
{
cout << "please Enter a proper integer value" << e << endl;
}
return 0;
}