-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexception1.cpp
More file actions
42 lines (39 loc) · 762 Bytes
/
Copy pathexception1.cpp
File metadata and controls
42 lines (39 loc) · 762 Bytes
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
//exception handling in
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int factorial(int N)
{
int f=1;
for(int i=N;i>0;i--)
{
f = f * i;
}
return f;
}
int main(int argc, char const *argv[])
{
int n;
string e = "Please Enter a Positive number!";
system("cls");
cout<<"Enter Number to Calculate factorial ";
cin>>n;
try //try block that may be throw error
{
if(n>= 0)
{
cout<<"Factorial is "<<factorial(n)<<endl;
}
else
{
throw e; //throwing error as e as defined above
}
}
catch(string s) //catching the error
{
cout<<s<<endl;
}
system("pause");
return 0;
}