CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Why the output?

  1. #1

    Why the output?

    Code:
    #include <iostream>
    #include <exception>
    
    using namespace std;
    
    void myunexpected () {
      cerr << "unexpected called\n";
      throw 0;     // throws int (in exception-specification)
    }
    
    void myfunction () throw (int) {
      throw 'x';   // throws char (not in exception-specification)
    }
    
    int main (void) {
      set_unexpected (myunexpected);
      try {
        myfunction();
      }
      catch (int) { cerr << "caught int\n"; }
    
      return 0;
    }
    Expected output:
    unexpected called
    caught int

    Actual Output: No output is displayed and the program aborts.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Why the output?

    With GCC/mingw, I get your expected output.

    Although to be honest, NOBODY uses exception specifiers, except maybe for the rare function/object that guarantees to throw nothing. Also, your aren't supposed to return from unexpected (unexpected is supposed to terminate the program), so you shouldn't throw from unexpected either.

    I can't tell you why you are not getting the "expected" behaviour, but I will tell you that you are going into real deep and obscure C++ semantics, and using them in ways they were not meant to be used. The resulting mix is that a definite answer will be very hard to give.

    What's your compiler? What are your options?
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3

    Re: Why the output?

    Compiler is MSDEV 2008

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Why the output?

    When compiling with VStudio, you get warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow). What this basically means is that the microsoft compiler hasn't implemented exception specifications (except no throw), and will be quite happy with throwing your char. What happens next though is that you do not catch that char, and then you terminate, because of an un-caught exception.

    try this:

    Code:
    #include <iostream>
    #include <exception>
    
    void myunexpected () {
      std::cerr << "unexpected called\n";
      throw 0;     // throws int (in exception-specification)
    }
    
    void myfunction () throw (int) {
      throw 'x';   // throws char (not in exception-specification)
    }
    
    int main (void) {
      std::set_unexpected (myunexpected);
      try {
        myfunction();
      }
      catch (int) { std::cerr << "caught int\n"; }
      catch (char) { std::cerr << "caught char\n"; }
    
      return 0;
    }
    Somebody with more VStudio experience can tell you how to force exception specifications, but I don't think it is even possible.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,153

    Re: Why the output?

    Visual Studio does not support exception specifications up to now.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)