Debugging Error

Hi there again! I have been learning c++ using addison wesley programming principles and practices using c++ 2nd edition. Below is a code i came up real quick using this particular library. However, when I run the code and enter my input as something that is not "5", I get a debug error and abort () has been called. It should read the line within the error function, and I don't understand why it isn't doing so.

1
2
3
4
5
6
7
8
9
10
11
 #include "../../std_lib_facilities.h"


int main()
{
	cout << "Please enter something you" << '\n';
	int x;
	cin >> x;
	if (x != 5) error("you are a poop");
	
}
However, when I run the code and enter my input as something that is not "5", I get a debug error and abort () has been called.


Yes, that's because you haven't defined your exception handlers. Try this (I'm not going to because I don't have Stroustrup's predefined header std_lib_facilities.h on hand):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "../../std_lib_facilities.h"

int main()
try {
	cout << "Please enter something you" << '\n';
	int x;
	cin >> x;
	if (x != 5) error("you are a poop");
}

catch (exception& e)
{
    cerr << e.what() << endl;
    exit(1);
}


EDIT: And if anyone else is reading this and doesn't understand/didn't read Stroustrup's book, he advises everyone to utilize a precompiled header he created specifically for the book PPP, which #includes most of the libraries needed to run the code in the book, defines namespace std, and also has a simple error handler.
Last edited on
Now it works great, thanks! If it isn't too much trouble, would you mind explaining exception handlers and the try/catch function you used? I really appreciate the help!

Edit: Thanks "Yawzheek" I just read your edited post. Thanks for the advice!
Last edited on
Ya know, I would love to, but Stroustrup does a FAR better job than I ever could in chapter 5, and I'll just give you half advice. If I could summarize it, I would say:

Basically what you're doing is setting up simplified error handling that will give you a better message than the garbled nonsense you'll usually get when you encounter run-time errors. The try aspect is just "defining" scope to look for them (I guess you would say?) and catch is just where errors are "caught" to give you a meaningful message to output. For example:

1
2
3
4
5
6
7
8
9
10
11
catch(exception& e)
{
   cerr << e.what() << endl;
   exit(1);
}

catch (...)
{
   cerr << "Unknown exception...\n";
   exit(1);
}


The first function is what your error("MESSAGE") goes to if you supply it. Your message should be something that identifies where the problem was found and/or what it was, and it's passed along. The second function is, as the output suggests, when an exception was caught, but it's not well known.

1
2
3
4
5
6
7
struct Month{
     int m;
     Month(int month):m{month}
     {
          if(m < 1 || m > 12) error("Month must be between 1 and 12");
     }
}

To be honest, I don't even like telling you that much because I don't know for certain the specifics, but that's what I know from using it, and I don't want to lead you down the wrong path. The other problem is that Stroustrup personalized it heavily for the text, so (as far as I am on chapter 19) I can't say for certain if he breaks it down further. Sorry :(

Also, you may find it useful (or possibly not), but I like to include __func__ (those are 2 '_' characters) to include the name of the function the error was found inside.
Last edited on
Gotya, this gives me a good fundamental idea of exception handlers. Thanks alot, I really appreciate it! I'll definitely check the chapter and section out when i get to it.
Topic archived. No new replies allowed.