-3

When I run this in linux machine, I expect the catch block to catch the exception. Whereas I get a segmentation fault. Why is this ? Should it not print "Caught exception"

void handle_eptr(std::exception_ptr eptr) // passing by value is ok
{
    try {
        if (eptr) {
            std::rethrow_exception(eptr);
        }
    } catch(const std::exception& e) {
        std::cout << "Caught exception \"" << e.what() << "\"\n";
    }
}

int main()
{

    char *c =(char*) 0x10;



    std::exception_ptr eptr;
    try {
        std::string s = c;

    } catch(...) {
        std::cout<< "Caught exception";
        //eptr = std::current_exception(); // capture
    }
   // handle_eptr(eptr);
    std::cout << "Normal Exit";
}
Ram
  • 389
  • 4
  • 13
  • 1
    [What exception](http://en.cppreference.com/w/cpp/string/basic_string/basic_string)? – LogicStuff Jun 20 '17 at 14:57
  • Should it not print "Caught exception" – Ram Jun 20 '17 at 14:59
  • 4
    your expectation is that an illegal address dereference will cause a c++ exception. This is not so. You have triggered UB, UB could do anything; including throwing an exception, turning on the lights on yr xmas tree, reformat yr hard drive, ... – pm100 Jun 20 '17 at 15:02
  • C++ is not like Java - there are some undefined behaviors (UBs) that will cause the program to crash without an exception. – Gillespie Jun 20 '17 at 15:05
  • 2
    I'm voting to close this question as off-topic because asking why made-up language rules do not hold isn't going to produce valuable insights. – IInspectable Jun 20 '17 at 15:06

4 Answers4

5

Given

char *c =(char*) 0x10;

the following line causes undefined behavior.

std::string s = c;

The behavior of your program after that can be anything. It's pointless to try make sense of the program's behavior after that.

More on undefined behavior can be found at Undefined, unspecified and implementation-defined behavior.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • But there was a catch to catch the exception. I thought it should print "Caught exception" message and gracefully return – Ram Jun 20 '17 at 15:00
  • 3
    @Ram there is no exception to be caught. *Undefined behavior* does not throw an exception – UnholySheep Jun 20 '17 at 15:01
  • 1
    @Ram, there is no guarantee that accessing memory that you are not supposed to must throw an exception. Some platforms might but then, that's also undefined behavior. – R Sahu Jun 20 '17 at 15:03
1

You are trying to initialize std::string with a *char who points to memory that is almost sure not for your program. You get a segmentation fault which is not a C++ exception it is system exception. C++ try.. catch blocks only capture C++ exceptions.

Petar Velev
  • 2,305
  • 12
  • 24
0

You are initializing a std::string with 0x10, where does that point? What you have is undefined behavior, which was luckily caught with a segmentation fault. C++ does not require exceptions to be thrown for such undefined behavior.

A segmentation fault does not cause an exception to be thrown. And that is why nothing is caught.

Curious
  • 20,870
  • 8
  • 61
  • 146
0

Exceptions are thrown by throw statements. Random coding errors might result in accidentally throwing exceptions, but that is not required and not at all common.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165