-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracticep106_exeption_hand_class_5.cpp
More file actions
52 lines (49 loc) · 1.24 KB
/
practicep106_exeption_hand_class_5.cpp
File metadata and controls
52 lines (49 loc) · 1.24 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;
// EException handling
//multiple conditions in exception handling(catch block)
class InvalidPasswordException{
public:
string password;
InvalidPasswordException(string a){
this->password=a;
};
void val(){
bool j=false;
if(password.length()<6){
throw 1;
};
for(int i=0;i<password.length();++i){
if(isdigit(password[i])){
j=true;
break;
}
};
if(j==false){
throw 2;
};
}
};
int main(){
string a;
string b;
getline(cin,a);
getline(cin,b);
try{
InvalidPasswordException e1(b);
e1.val();
cout<<"Password is correct"<<endl;
}
catch (int e) {
if (e == 1) {
// Password length is too short
cout << "Password must be at least 6 characters long." << endl;
cout << "Caught by exception, invalid password format!" << endl;
}
else if (e == 2) {
// Password doesn't contain a digit
cout << "Password must have at least one digit (0-9)" << endl;
cout << "Caught by exception, invalid password format!" << endl;
}
};
}