I do not quite understand how exception handling is done exactly. I understand the "big picture" of what it is supposed to do, but I am having trouble making it work.
Here is a simple program. I kept it small for the sake of simplicity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <math>
int main()
{
int var1=0;
cout<<"please enter in a number (positive only)."<< endl;
try
{
cin>>var1;
}
catch (var1<0))
{
cout<<"negative numbers are not allowed"<<endl;
}
return 0;
}
The gist of this program is that I want someone to enter in a number that is positive only. If they type in a negative number, I want the catch block to throw up the message telling the user that negative numbers are not allowed. Please advise me on what I am doing wrong.
Exception handling is for handling exceptions; it is not for things that are normal. Normal things, such as checking if an integer is less than zero, are not exceptions. In C++ you can throw pretty much anything, but people usually just throw some derivative of an exception class object. http://www.cplusplus.com/doc/tutorial/exceptions/
I actually have seen that link, but I'm confused by the first example. See line in bold in the code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// exceptions
#include <iostream>
usingnamespace std;
int main () {
try
{
throw 20;
}
catch (int e) //I didn't realize that the bold wasn't going to show up very well. this is the line that confuses me.
{
cout << "An exception occurred. Exception Nr. " << e << '\n';
}
return 0;
}
The code says "Catch (int e)" but I do not understand entirely what this line means or the significance of it. This is where I am struggling.
Please note: I am not a C++ expert. I am simply some guy trying to teach himself C++. All comments/feedback/etc are welcomed.
int main()
{
int var1 = 0;
cin >> var1;
try
{
if (var1 < 0){throw 1;}
}
catch (int x)
{
cout << "Error Number: " << x << ". Your value: " << var1 << ". Is not valid." << endl;
}
system("Pause");
return 0;
}
This works without giving me any issues. For reasons that I do not understand, "catch (int x)" takes a value of 1. Why is this referencing the number in my throw value?
Are there any advantages/disadvantages to throwing an exception class rather than an int?
http://www.cplusplus.com/reference/exception/
"This header <exception> defines the base class for all exceptions thrown by the elements of the standard library: exception, along with several types and utilities to assist handling exceptions"
My experience with C++ is limited to being on like chapter five of the book I'm reading
Exceptions aren't really a beginner topic. Honestly I would say don't worry too much about them yet. They're a pretty complex topic to understand properly, and you're almost certainly going to misuse them as a beginner.
Are there any advantages/disadvantages to throwing an exception class rather than an int?
It's almost entirely advantageous to throw proper exception objects, but it'd be difficult to explain why without getting into a big spiel about how exceptions are typically used -- which I'm too tired to do right now =P