Ending Function on Error then Continue in main()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • - Steve -

    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::IntAr ray(int low, int high)
    {
    arrayLow=low; //start of array index
    arrayHigh=high; //end of array index
    if(arrayHigh>=a rrayLow)
    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
    }


  • - Steve -

    #2
    Re: Ending Function on Error then Continue in main()


    "Heinz Ozwirk" <wansor42@gmx.d e> wrote in message
    news:bg4vq4$d7a $02$1@news.t-online.com...
    "- Steve -" <sevans@foundat ion.sdsu.edu> schrieb im Newsbeitrag
    news:t0mVa.9598 4$R92.85604@new s2.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?
    [color=blue][color=green]
    >>Avoid reporting errors on the screen in functions you (or others) might[/color][/color]
    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.
    [color=blue][color=green]
    >>Regards
    >> Heinz[/color][/color]

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




    Comment

    • Karl Heinz Buchegger

      #3
      Re: Ending Function on Error then Continue in main()



      - Steve - wrote:[color=blue]
      >
      > 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::IntAr ray(int low, int high)
      > {
      > arrayLow=low; //start of array index
      > arrayHigh=high; //end of array index
      > if(arrayHigh>=a rrayLow)
      > 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
      > }[/color]


      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
      kbuchegg@gascad .at

      Comment

      • Simon Turner

        #4
        Re: Ending Function on Error then Continue in main()

        "- Steve -" <sevans@foundat ion.sdsu.edu> wrote in message
        news:<c%nVa.967 84$R92.51701@ne ws2.central.cox .net>...[color=blue]
        > "Heinz Ozwirk" <wansor42@gmx.d e> wrote in message
        > news:bg4vq4$d7a $02$1@news.t-online.com... "- Steve -"
        > <sevans@foundat ion.sdsu.edu> schrieb im Newsbeitrag
        > news:t0mVa.9598 4$R92.85604@new s2.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?
        >[color=green][color=darkred]
        > >> 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.[/color][/color]
        >[color=green][color=darkred]
        > >>Regards Heinz[/color][/color]
        >
        > Unfortantley it's a requirment for the assignment I've been given.[/color]

        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_erro r("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::IntAr ray
        // throws std::runtime_er ror, skipping the rest of
        // test17().
        }
        }

        Comment

        Working...