-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathException.cpp
More file actions
54 lines (51 loc) · 976 Bytes
/
Exception.cpp
File metadata and controls
54 lines (51 loc) · 976 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
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <exception>
#include <iostream>
#include <unistd.h>
using namespace std;
class check_exception : exception {
private:
int health;
public :
const char* what() {
return " Game over ... " ;
}
void get_health() {
cout << " the health level is " << health <<endl;
}
void set_health( int SetHealth) {
this->health = SetHealth;
}
};
class GameObject_health {
private :
int health;
public :
GameObject_health(){
health = 10;
cout << " The current health Level is "<< health <<endl;
}
void health_calc() {
while(true){
sleep(1);
//health -=1;
health = random() % 10;
cout << " The health is :" << health <<endl;
if (health == 0) {
check_exception chx;
chx.set_health(health);
throw chx;
}
}
}
};
int main(int agvc, char **argv){
GameObject_health gh;
try{
gh.health_calc();
}catch(check_exception ch_x){
cout << ch_x.what() << endl;
ch_x.get_health();
}
return(EXIT_SUCCESS);
}