I hava a problem with my exceptional handling

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
//string::find
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <string.h>
#include <iomanip.h>
using namespace std;
class Ticket
{protected:
 int type;
 string number;
 public:
        
Ticket(){ cout<< "enter number"<<endl; cin>>number;              }
void set_number()  {cout<<" enter the number of winning ticket"<<endl;
                  cin>>number;
                  } // end of set_type

string get_number(){return number;} // end of get_type
     
}; 
class Lottery:public Ticket
{

string a[35];
public:
Lottery(){ifstream read("win.txt", ios::in);
          for(int i=1; i<36;i++)
                  {read>>a[i];
                  cout<<a[i]<<endl;
                  }
         } // end of lottery constructor
void search(){
     
              for(int i=1; i<36; i++)
              {if(a[i]==number)
              {cout<<"you won"<<endl;
              throw Ex(number);}
              
              }
     
              } // end of search function

};
Ex{
 string j;
 public:
 Ex(string m){ j=m;            }
 void print() { cout<<j<<" sehryer is okey"                };
              
              
};


int main()
{
Lottery lucky;
try
{
lucky.search();

}
catch (Ex &x)
{
  x.print();
};
  
system("pause");
return 0;
} // end of main; 




ı did not understand why this code is not working, because when ı throw normal string it is okey. However, when ı want to throw exceptional class, it shows to me undeclared exceptional class..
throw Ex(number);

What kind of an object is Ex? The compiler does not know, because it has never seen that kind of an object. In C++, the compiler must know about an object before you try to use it.

Ex{
If this is meant to be defining the class Ex, you need to use the class keyword.
class Ex{
Last edited on
ı did definition of class for ex. I did not understand you what did you want to say sorry:(
can you give me an example
My fault; I was unclear. I meant you must tell the compiler about it (either by defining or declaring it) before you try to use it.
but there is no need to declare it. ı did class definition ...
how can ı do this declaration?
can anyone help me?
ı did class definition ...


Yes you did. Did you do it before you tried to use it? No.

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
//string::find
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <iomanip>
using namespace std;

class Ex{
 string j;
 public:
 Ex(string m){ j=m;            }
  void print() { cout<<j<<" sehryer is okey"    ;            };
              
              
};


class Ticket
{protected:
 int type;
 string number;
 public:
        
Ticket(){ cout<< "enter number"<<endl; cin>>number;              }
void set_number()  {cout<<" enter the number of winning ticket"<<endl;
                  cin>>number;
                  } // end of set_type

string get_number(){return number;} // end of get_type
     
}; 
class Lottery:public Ticket
{

string a[35];
public:
Lottery(){ifstream read("win.txt", ios::in);
          for(int i=1; i<36;i++)
                  {read>>a[i];
                  cout<<a[i]<<endl;
                  }
         } // end of lottery constructor
void search(){
     
              for(int i=1; i<36; i++)
              {if(a[i]==number)
              {cout<<"you won"<<endl;
              throw Ex(number);}
              
              }
     
              } // end of search function

};



int main()
{
Lottery lucky;
try
{
lucky.search();

}
catch (Ex &x)
{
  x.print();
};
  
system("pause");
return 0;
} // end of main;  


Please do note that using exceptions instead of proper (and much simpler) function return values is expensive and generally considered bad practice.
Last edited on
you can not be serious :)
this can not be such a easy :))
thanks hahha;

this is good point.... ı understood now:)
Last edited on
Topic archived. No new replies allowed.