"enki" <en*****@yahoo.com> wrote...
I am trying to understand try throw and catch. I am trying to write a
simple demo porgram to see how it works. This is what I have so far:
#include<iostream>
using namespace std;
int main(){
int x;
try{
cin>>x;
}
catch(x){
cerr<<"Must be int!\n";
x = 0;
}
return 0;
}
It's not the best way to understand *exceptions* ('exemptions' is something
completely different). 'cin's operator >> does not throw anything when you
don't provide the right input. It just goes into a bad state.
Here is a simple example:
#include <iostream>
using namespace std;
void foo(int x) throw(int)
{
throw x;
}
int main()
{
try
{
foo(42);
}
catch (int x)
{
cout << "caught " << x << endl;
}
}
V