473,383 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Exemptions?

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
7 1223
Oops, I didn't mean to post this here.

Jul 23 '05 #2
"enki" <en*****@yahoo.com> wrote...
Oops, I didn't mean to post this here.


Really? Why?
Jul 23 '05 #3
"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
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
* 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
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
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bathroom_Monkey | last post by:
For posterity's sake, here is an algorithm I created to take a GMT time and convert it to U.S. central time, accounting for daylight saving time. Note: this algorithm can be modified to work for...
35
by: Michael Kearns | last post by:
I've been using python to write a simple 'launcher' for one of our Java applications for quite a while now. I recently updated it to use python 2.4, and all seemed well. Today, one of my...
2
by: Citoyen du Monde | last post by:
Trying to get some ideas on a simple javascript project (to teach myself the language). I want to develop a client-side vocabulary practice application that would allow users to enter their own...
34
by: Chris Hills | last post by:
MISRA is looking at doing a MISRA-C++ This is because they were asked to do it by a lot of people in industry. They are looking for some people to make up the team (based in the UK) The team...
8
by: brian.digipimp | last post by:
I turned this in for my programming fundamentals class for our second exam. I am a c++ newb, this is my first class I've taken. I got a good grade on this project I'm just wondering if there is a...
5
by: John Sheppard | last post by:
Hi there Does anyone know how to fill a bound textbox automaticlly. In an unbound textbox I would put in the control source =Sum(price, tax) (or some such function) and access updates it...
0
by: DANIEL HENEGHAN | last post by:
From Norm Matloff's newsletter To: H-1B/L-1/offshoring e-newsletter Any time you see a bunch of newspaper and magazine editorials that call on Congress to raise the H-1B, you can count on more...
6
by: Brian | last post by:
I wrote this program to calculate a users gross pay based on marital status and the number of exemptions they have. I have a few questions about my functions, heres my code: #include <iostream>...
4
by: mskichu | last post by:
hi, I have a xml in which I want to replace the element(s) value with XSLT Xml message <Message> <case> <party1> <!-- there are other elements -->
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.