I have the following code for extracting a string out of a buffer array.
It works fine. However, the length variable is determined at runtime, so if it were to go out of bound, an exception would occur. Of course, the code can be easily adjusted to check if the length variable (in relation to the offset variable) falls within the boundaries. Though I’m curious why the following code does not work, as the exception seems to fly through the try-catch statement (and get caught by the debugger).
try
{
string value(&buffer[offset], length);
// ...
}
catch (exception& e)
{
// ...
}
catch (...)
{
// ...
}
Running on Windows 7 64bit, MSVCR compiled.
Accessing the
bufferarray beyond bounds is undefined behaviour. There is no requirement for a C++ exception to be thrown in this case. Try instantiating the string outside a try block and you’ll see for sure whether an exception is thrown.