Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Ending Function on Error then Continue in main()

Reply
Thread Tools

Ending Function on Error then Continue in main()

 
 
- Steve -
Guest
Posts: n/a
 
      07-29-2003
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
}


 
Reply With Quote
 
 
 
 
- Steve -
Guest
Posts: n/a
 
      07-29-2003

"Heinz Ozwirk" <> wrote in message
news:bg4vq4$d7a$02$...
"- Steve -" <> schrieb im Newsbeitrag
news:t0mVa.95984$.. .
: 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.




 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      07-29-2003


- 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

 
Reply With Quote
 
Simon Turner
Guest
Posts: n/a
 
      07-29-2003
"- Steve -" <> wrote in message
news:<c%nVa.96784$> ...
> "Heinz Ozwirk" <> wrote in message
> news:bg4vq4$d7a$02$... "- Steve -"
> <> schrieb im Newsbeitrag
> news:t0mVa.95984$.. .
> : 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().
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
growing <div>s keeping then ending at the same stop jason@cyberpine.com HTML 0 09-25-2006 10:21 AM
Help. SessionID is x then y then x then y BodiKlamph@gmail.com ASP General 0 09-03-2005 03:02 PM
javascript to check the text of a textbox and then continue buran ASP .Net 2 03-28-2005 02:01 PM
greater then / less then =?Utf-8?B?TWlrZQ==?= ASP .Net 2 11-04-2004 06:05 PM
I'm a newbie: need to script "init S", then continue running code Kafer Perl Misc 3 10-08-2003 06:35 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57