Exemptions?

E

enki

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;
}
 
V

Victor Bazarov

enki said:
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
 
A

Alf P. Steinbach

* enki:
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;

Here, somewhere, before first use of std::cin, tell it to use
exceptions by e.g.

std::cin.exceptions( std::ios_base::iostate( -1 ) );

(see <url:
http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01_02_08.html>.

But I only recommend this technique for small example programs where
the program exits -- in some way -- if an exception is thrown.

try{
cin>>x;
}

catch(x){

The thrown exception will be of type std::exception or a derived class,
and you catch an exception by specifying the type (and possibly also an
invented name, like a variable declaration) so catch it like

catch( std::exception const& )

For this to work you need to

#include <stdexcept>

at the top of the program.
 
E

enki

I have been reading the posts and reading some. I am starting to
almost get it. I got a program to compile but not work well. I did
this program before I real all the replys. I see some mistakes but it
is starting to get better.

#include<iostream>
#include<string>


using namespace std;

struct error{
char *_msg;
error(char *msg){ _msg = msg;}
};



int main(){

int x;
while(x != 0){
cout<<"Number:";
try{
cin>>x;
}

catch(error e){
cout<<"Must be int!\n";

x = 0;
}
cout<<x<<"\n";
}
system("pause");
return 0;
}
 
A

Alf P. Steinbach

* enki:
I have been reading the posts and reading some. I am starting to
almost get it. I got a program to compile but not work well. I did
this program before I real all the replys. I see some mistakes but it
is starting to get better.

What was the _first_ thing in my reply you didn't understand?

[Code snipped]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,270
Messages
2,571,353
Members
48,041
Latest member
Oliwen826

Latest Threads

Top