c ++: Doubt with exception handling

3

I'm learning c ++ and I wanted to try a code using try and catch. So I wrote a program that would deliberately send an error out_of_range:

#include <iostream>
#include <stdexcept>

class foo{
    public:
        foo(int s) :elem{new double[s]} {}//constructor de foo
        double& operator[](int i);
    private:
        double* elem;
};

double& foo::operator[](int i)
{
    try{
        std::cout << "se accedio a elem[" << i << "] satisfactoriamente\n";
        return elem[i];
    }
    catch(std::out_of_range){
        std::cout << "Ups " << i << " esta fuera del rango de elem\n";
        throw std::out_of_range{"Vector::operator[]"};
    }
}

void error() //Esta función esta creada para deliberadamente lanzar el error out_of_range
{
    foo boo(1);
    boo[1] = 1;
    boo[2] = 2;    
}

int main()
{
    error();
}

The problem is that even when you try to write a value in boo [1] and boo [2] the output prints

se accedio a elem[1] satisfactoriamente

and continue without throwing any errors.

(It seems that when I try to throw errors deliberately I do not get any: P)

    
asked by MahaSaka 23.03.2018 в 00:24
source

1 answer

4

You have a confusion. One thing is the exceptions of the language, and another the exceptions of the operating system.

Operating system exceptions are thrown by this out of the control of your program , and are handled differently on each platform, using the API of the operating system itself; there is no way, from C ++ pure , to control them.

A simple test:

#include <iostream>

int main( ) {
  int *j = nullptr;

  try {
    *j = 10;
    return 0;
  } catch( ... ) {
    std::cout << "Huy !!\n";
  }

  return 0;
}

The exceptions of the language must be released by you , and are completely independent of the operating system. Its use is to facilitate readability:

#include <iostream>
#include <stdexcept>

class foo{
  public:
    foo( int s ) :
      elem{ new double[s] },
      count{ s } {
    } // constructor de foo
    double& operator[]( int i );

  private:
    double* elem;
    int count;
};

double& foo::operator[]( int i ) {
  if( i > count ) throw std::out_of_range( "Te pasaste !!\n" );

  std::cout << "se accedio a elem[" << i << "] satisfactoriamente\n";
  return elem[i];
}

void error( ) {
  try {
    foo boo(1);
    boo[1] = 1;
    boo[2] = 2;    
  } catch( std::out_of_range &e ) {
    std::cout << "Huy !! ";
    std::cout << e.what( );
  }
}

int main( ) {
  error( );

  return 0;
}

No error occurs in your original code because the memory you reserve does not always match the size you request. The S.O. reserve memory in a few blocks of minimum size; you can request 8 bytes and the system, internally, can reserve 32 bytes . This is done to facilitate the internal management of memory.

Also be aware that accessing random memory positions will not always cause an exception of the S.O. ; then go to fall in memory that you have reserved yourself previously. In these cases, you will corrupt internal values, but not always will cause an exception of the S.O.

    
answered by 23.03.2018 / 05:27
source