473,503 Members | 2,238 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ending Function on Error then Continue in main()

I have a situtation where if a overloaded operator is used incorrectly I
need it to cout some info to the screen, end the function it was called in,
and continue on in main. How on earth do you do that?

The exact example can be found at http://planetevans.com/c under test 17,
18, and 19.

One of the specific examples is

test17()
{
cout << "17. Array declared with illegal array bounds: IntArray f(5,
2);" << endl << endl;
IntArray f(5, 2); //illegal becuase it is trying to define an
array with an index going from 5 to 2. The constructor is show below
for(int i = f.low(); i <= f.high(); ++i) //dont want this to run
f[i] = i * 10; //dont want this to run
cout << f << endl; //dont want this to run
}

IntArray::IntArray(int low, int high)
{
arrayLow=low; //start of array index
arrayHigh=high; //end of array index
if(arrayHigh>=arrayLow)
array=new int[arrayHigh-arrayLow+1]; //create array memory
locations
else
cout << "Low Array Boundry Higher than High Array Boundry" << endl;
//now I need to end the function that this was called from
}
Jul 19 '05 #1
3 2912

"Heinz Ozwirk" <wa******@gmx.de> wrote in message
news:bg*************@news.t-online.com...
"- Steve -" <se****@foundation.sdsu.edu> schrieb im Newsbeitrag
news:t0*******************@news2.central.cox.net.. .
: I have a situtation where if a overloaded operator is used incorrectly I
: need it to cout some info to the screen, end the function it was called
in,
: and continue on in main. How on earth do you do that?
Avoid reporting errors on the screen in functions you (or others) might use again in other programs. Report them to the calling program and let the
program (and >>its programmer) decide how to handle them. That's what
exceptions have been invented for. Have a look at try/catch/throw and
std::exception.
Regards
Heinz


Unfortantley it's a requirment for the assignment I've been given.


Jul 19 '05 #2


- Steve - wrote:

test17()
{
cout << "17. Array declared with illegal array bounds: IntArray f(5,
2);" << endl << endl;
IntArray f(5, 2); //illegal becuase it is trying to define an
array with an index going from 5 to 2. The constructor is show below
for(int i = f.low(); i <= f.high(); ++i) //dont want this to run
f[i] = i * 10; //dont want this to run
cout << f << endl; //dont want this to run
}

IntArray::IntArray(int low, int high)
{
arrayLow=low; //start of array index
arrayHigh=high; //end of array index
if(arrayHigh>=arrayLow)
array=new int[arrayHigh-arrayLow+1]; //create array memory
locations
else
cout << "Low Array Boundry Higher than High Array Boundry" << endl;
//now I need to end the function that this was called from
}

As Heinz has already told you: you could throw an exception in the constructor.
Another possibility would be:

class IntArray
{
public:
IntArray( int LowBound, int HighBound );

bool IsGood();

...
};

void test17()
{
IntArray f( 5, 2 );

if( !f.IsGood() )
return;

...
}
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #3
"- Steve -" <se****@foundation.sdsu.edu> wrote in message
news:<c%*******************@news2.central.cox.net> ...
"Heinz Ozwirk" <wa******@gmx.de> wrote in message
news:bg*************@news.t-online.com... "- Steve -"
<se****@foundation.sdsu.edu> schrieb im Newsbeitrag
news:t0*******************@news2.central.cox.net.. .
: I have a situtation where if a overloaded operator is used
: incorrectly I need it to cout some info to the screen, end the
: function it was called in, and continue on in main. How on earth do
: you do that?
Avoid reporting errors on the screen in functions you (or others)
might use again in other programs. Report them to the calling
program and let the program (and its programmer) decide how to
handle them. That's what exceptions have been invented for. Have a
look at try/catch/throw and std::exception.Regards Heinz


Unfortantley it's a requirment for the assignment I've been given.


The "Avoid reporting errors on the screen..." is a recommendation that
you should avoid it in general. If you need to do it here, then do it.

The advice on using exceptions is orthogonal, and is what you need.
Note that it isn't an overloaded operator you're using incorrectly, but
the constructor. Anyway, your code should probably look like:

#include <stdexcept>

class IntArray
{
public:
IntArray(int low, int high)
{
if( low > high )
// add output here if you need it
throw std::range_error("IntArray: low > high");

//...your code...
}
};

void test17()
{
IntArray f(5,2); //this will throw straight away

//...this code will not be reached...
}

int main()
{
try {
//...
test17();
//...

} catch( std::exception& ex ) {
// we come straight here after IntArray::IntArray
// throws std::runtime_error, skipping the rest of
// test17().
}
}
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1880
by: muser | last post by:
In the following function there is an access violation error, some memory can't be read. A week ago this code did compile. Can anyone possibly tell me why my compiler is unable to read part of...
38
2496
by: Red Dragon | last post by:
I am self study C student. I got stuck in the program below on quadratic equation and will be most grateful if someone could help me to unravel the mystery. Why does the computer refuse to execute...
27
2048
by: cj | last post by:
I run this program and to exit click the X in the upper right corner. But apparently it isn't really ending the program. If I return to VB and make changes then try to rebuild the app it says the...
10
10016
by: morangolds | last post by:
Hi, I've been having a problem with C++ Windows Forms apps not "ending" when you close the form window. I've searched about this problem all over the place and most searches have lead me to...
4
2177
by: wesbland | last post by:
>From my understanding, when a string is stored in VB.NET and you look at it in the debugger, it has a quote on both sides to signify that it is a string as opposed to a char or int or whatever. ...
1
1744
by: Randeh | last post by:
Newb C++ problem, arithmetic functions are returning 0 values each time. Not sure where the problem is, but I've run checks and it seems to be going fine until the very end where the arithmetic...
34
2699
by: Umesh | last post by:
I want to extract a string abc*xyz from a text file. * indicates arbitrary no. of characters. I'm only able to do it when the string has definite no. of characters or the string length is...
2
1373
by: Alenik1989 | last post by:
I wrote this code, but I'm not able to test it because im getting 3 errors. So I'm not sure that this code will work properly or not!!! the code in #include <stdio.h> #include <stdlib.h> int...
4
3276
by: istillshine | last post by:
I have a function foo, shown below. Is it a good idea to test each argument against my assumption? I think it is safer. However, I notice that people usually don't test the validity of...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.