Knowing which exception to catch

I am not fully understanding exception handling.

My situation is as follows. I am trying to take an input to a program and parse it into an int. I am using atof to do this. However, the user may not have provided an int to the program and I want to catch those exceptions and terminate the program. However, I cannot figure out what kind of exception atof throws and how to store it so as to throw it. I know I need something like the following.

1
2
3
4
5
6
7
8
9
...
std::string input;
try{
    int num atof(input.c_str());
}
catch(<exception here>){
    cout << "Invalid input" << endl;
    exit(0);
}


atof is a C function and therefore does not throw any exceptions.
See http://www.cplusplus.com/reference/clibrary/cstdlib/atof/
If you want to use a function that throws exceptions, use boost's lexical_cast.
~Note that num's scope is only within the try bracket~

Here's a basic exception:
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
#include <iostream>
using namespace std;

double divide(double a, double b)
{
  if (b == 0)
  {
    throw "Can not divide by zero\n";
  }
  
  // This only happens if b != 0
  return a / b;
}

int main()
{
  double x, y;
  cout << "Enter x: ";
  cin >> x;
  cout << "Enter y: ";
  cin >> y;

  double z;
  try
  {
    z = divide(x, y);
  }
  catch (const char *exception)
  {
    cout << exception;
    z = 0;  // this line makes the program fix itself enough to run smoothly
  }

  cout << "z equals: " << z << endl;
 
  return 0;
}


See that exceptions can be thrown more than once and with more than one type:
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
#include <iostream>
using namespace std;

double divide(double a, double b)
{
  if (b == 0)
  {
    throw "Can not divide by zero\n";
  }
  else if (b == 1)
  {
    cout << "Why divide by one?\n";
    throw a;
  }
  
  // This only happens if b != 0 or 1
  return a / b;
}

int main()
{
  double x, y;
  cout << "Enter x: ";
  cin >> x;
  cout << "Enter y: ";
  cin >> y;

  double z;
  try
  {
    z = divide(x, y);
  }
  catch (const char *exception)
  {
    cout << exception;
    z = 0;
  }
  catch (double dividedByOne)
  {
    z = dividedByOne;
  }

  cout << "z equals: " << z << endl;
 
  return 0;
}
Last edited on
cannot figure out what kind of exception atof throws and how to store it so as to throw it.


atof, like other atoX functions, is notorious for the lack of error handling. Even in C, strtof() and others are the better choice.

In C++, stof() throws as you expect:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <iostream>
int main()
{
    std::string input;
    std::cin >> input;
    try{
        float num = std::stof(input);
    }
    catch(const std::exception& e){
        std::cout << "Invalid input: " << e.what() << '\n';
        return 1;
   } 
}

online demo: http://ideone.com/hGCN7

Here I catch std::exception&, but if you want to be specific, stof() throws std::invalid_argument or std::out_of_range, depending on the kind of error it encounters.

If your compiler is too old for stof(), there's always boost::lexical_cast as already mentioned (it throws boost::bad_lexical_cast, which you can also catch as a std::exception&, or as a std::bad_cast& if you like)
Last edited on
Topic archived. No new replies allowed.