-
Notifications
You must be signed in to change notification settings - Fork 0
/
fact.cpp
68 lines (59 loc) · 871 Bytes
/
fact.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
/* C++ program to demonstrate custom exception handling
* Written By: Ramchandra Shahi Thakuri
* Date:
*/
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
void fact(int);
class ide
{
public:
char str_what[80];
int what;
ide()
{
*str_what = 0;
what = 0;
}
ide(char *s, int e)
{
strcpy(str_what, s);
what = e;
}
};
int main()
{
int n;
system("clear");
cout << "\t\t\tC++ program to demonstrate custom exception handling program\n";
cout << "Enter value of n: ";
cin >> n;
try
{
if(n < 0)
{
throw ide("Negative number entered", n);
}
else
fact(n);
}
catch(ide e)
{
cout << e.str_what << " : ";
cout << e.what << endl;
}
}
void fact(int n)
{
int res;
res = 1;
cout << "\nFactorial of " << n;
while( n != 0)
{
res *= n;
n--;
}
cout << " is " << res << endl;
}