By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

Exemptions?

P: n/a
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;
}

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Oops, I didn't mean to post this here.

Jul 23 '05 #2

P: n/a
"enki" <en*****@yahoo.com> wrote...
Oops, I didn't mean to post this here.


Really? Why?
Jul 23 '05 #3

P: n/a
"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
Jul 23 '05 #4

P: n/a
Victor Bazarov wrote:
"enki" <en*****@yahoo.com> wrote...
Oops, I didn't mean to post this here.


Really? Why?


Because your flames have created a climate of fear.
Jul 23 '05 #5

P: n/a
* 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.
cerr<<"Must be int!\n";
x = 0;
}
return 0;
}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #6

P: n/a
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;
}

Jul 23 '05 #7

P: n/a
* 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]

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.