catching error in c++

It is not reading the cathing errors in any way. It just ignores it. Can some one look this over and tell me why?

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

/***********************************************************
 * READ DATA: Read the data from a file
 *   INPUT:  fileName. the file to be read
 *   OUTPUT: data.  The data from the file
 * IMPORTANT: You are not allowed to change readData() in any way
 ***********************************************************/
string readData(const string & fileName) throw (string, bool, int)
{
   // missing file case
   if (fileName.size() == 0)
      throw true;             // "ERROR: No filename specified\n"

   // attempt to open the file
   ifstream fin(fileName.c_str());
   if (fin.fail())
      throw fileName;         //  "ERROR: Invalid filename \"" << s << "\"\n"

   // attempt to read the data
   string data;
   getline(fin, data);
   bool moreData = !fin.eof();
   fin.close();

   // empty file
   if (data.size() == 0)
      throw 0;                // "ERROR: The file was empty\n"

   // message too long
   if (data.size() > 140)
      throw 1;                // "ERROR: The message exceeded 140 characters\n"

   // more than one line of data
   if (moreData)
      throw 2;                // "ERROR: The message was longer than 1 line\n"

   // success
   return data;
}

/**********************************************************************
 * MAIN: This function will prompt the user for the file and display the
 *       contents on the screen.
 ***********************************************************************/
int main()
{
   // get the filename
   string fileName;
   cout << "What is the filename? ";
   getline(cin, fileName);

   // read the data
   string data = readData(fileName);

   try // is this right?
   {
      string readData(fileName);
   }

   catch (int integer)// also check to see if i did it right here correctly
   {

      switch (integer)
      {
         case 0:
            cout << "ERROR: The file was empty\n";
            break;
         case 1:
            cout << "ERROR: The message exceeded 140 characters\n";
            break;
         case 2:
            cout << "ERROR: The message was longer than 1 line\n";
            break;
      }
   }
   
   // display the results:
   cout << "The important fact: \""
        << data
        << "\"\n";
   
   return 0;
}

any ideas
Change your try block to:

1
2
3
4
5
6
   
   string data;
   try // is this right?
   {
     data = readData(fileName);
   }


The try block needs to be around the code that can throw.
thanks for the idea i figured it out
Topic archived. No new replies allowed.