Exceptions aren't standard exceptions 
Author Message
 Exceptions aren't standard exceptions

        I'm using the October 1998 release of the 6.0 compiler. I'm
trying to get a std::string to generate an std::exception descendant
with no luck.

01      try
02      {
03          std::string z("abc");
04          z[1000] = ' ';      // Should throw out_of_range.
05      }
06      catch (std::exception& e)
07      {
08          std::cout << "exception: " << e.what << endl;
09      }
10      catch (std::out_of_range& e)
11      {
12          std::cout << "out_of_range: " << e.what << endl;
13      }
14      catch (...)
15      {
16          std::cout << "unexpected" << endl;
17      }

Line 04 does generate an exception, but not the out_of_range exception
promised in the basic_string<> documentation. Line 16 executes. Has
anyone had this problem or know the solution?

Thanks,
Wes



Tue, 24 Apr 2001 03:00:00 GMT  
 Exceptions aren't standard exceptions

Quote:

>    I'm using the October 1998 release of the 6.0 compiler. I'm
>trying to get a std::string to generate an std::exception descendant
>with no luck.

>01  try
>02  {
>03      std::string z("abc");
>04      z[1000] = ' ';      // Should throw out_of_range.
>05  }
>06  catch (std::exception& e)
>07  {
>08      std::cout << "exception: " << e.what << endl;
>09  }
>10  catch (std::out_of_range& e)
>11  {
>12      std::cout << "out_of_range: " << e.what << endl;
>13  }
>14  catch (...)
>15  {
>16      std::cout << "unexpected" << endl;
>17  }

>Line 04 does generate an exception, but not the out_of_range exception
>promised in the basic_string<> documentation. Line 16 executes. Has
>anyone had this problem or know the solution?

What documentation are you referring to? Passing an invalid position to
basic_string::operator[] results in undefined behavior. You need to use
basic_string::at to get range-checking.

As a side-note, the "exception" you get above is a Win32 structured
exception, a non-C++ exception, inappropriately caught by catch(...). Also,
the compiler should have at least warned that your out_of_range handler can
never be entered. And you need to say e.what().

--
Doug Harrison



Tue, 24 Apr 2001 03:00:00 GMT  
 Exceptions aren't standard exceptions
Doug,

Quote:
>What documentation are you referring to? Passing an invalid position to
>basic_string::operator[] results in undefined behavior. You need to use
>basic_string::at to get range-checking.

        That got it. Thanks much.

Quote:
>As a side-note, the "exception" you get above is a Win32 structured
>exception, a non-C++ exception, inappropriately caught by catch(...). Also,
>the compiler should have at least warned that your out_of_range handler can
>never be entered. And you need to say e.what().

        My bad. Rather than copy my example, I hurriedly retyped it --
accidentally swapping the catch blocks.

Wes



Tue, 24 Apr 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Translate a SEH exception into a C++ exception

2. Soap Exception -- Inner Exception

3. Exception gurus - Copy constructors of exceptions

4. Exception with no Exception

5. Crash In Exception Filter in Structured Exception HAndling

6. Catching hardware exceptions using C++ exceptions

7. Catching SEC Exceptions as C++ Exceptions

8. Win32 structured exceptions vs C++ exceptions

9. retrieve exception context in C++ exception handler

10. Floating Point Exceptions: MFC or Structured Exception Handling?

11. Exceptions : Combining C++ and Structured Exception Handling ?

12. Exception handling - how to know which exceptions are thrown

 

 
Powered by phpBB® Forum Software