Thread: set_unexpected() Didn't work

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    103

    set_unexpected() Didn't work

    Hi

    I was seeing the behavior of unexpected() function in exception handling.

    I was wondering why the function set for un_expected() didn't called when exception was thrown.

    Below is the code:

    Code:
    #include <iostream>       
    #include <exception>     
    
    void myunexpected () {
      std::cerr << "unexpected called\n";
      throw 0;     /
    }
    
    void myfunction () throw (int) {
      throw 'x';   
    }
    
    int main (void) {
      std::set_unexpected (myunexpected);
      try {
        myfunction();
      }
      catch (int) { std::cerr << "caught int\n"; }
      return 0;
    }
    I was expecting the set_unexpected() should call myunexpected() and throw an int exception for the handler is there.

    I put debugger, but myunexpected() was not called.

    Is something wrong with code implementation or it is run time behavior of set_unexpected() function?

    If it is so, then what's the use of set_unexpected() ?

    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    One explanation is that the slash on the end of your line 6 would prevent the code compiling. Some IDEs, if you select "Run", will run an older version (previously compiled) of the executable in such circumstances.

    Even if your code does compile/link, debuggers don't always step into, or even permit stepping into, functions called during stack unwinding once an exception has been thrown. Once consequence of that is, when stepping with a debugger, it will not step into your myunexpected() function.

    Try doing a Build/Clean and Rebuild and (if that succeeds) run your program to completion, and see what output you get.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Thanks..

    It was copy/paste error.
    I clean sol and rebuild it.. and the result was same.Program was terminated abnormally.

    I didn't debug this time but the function myunexpected() was not called.

    Thanks

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you're using a Microsoft compiler, there's a fair chance it doesn't support exception specifications (i.e. it ignores them). That would explain the behaviour.

    That's no longer considered a problem, as exception specifications were deprecated in the latest C++ standard (C++11). They were never particularly useful to begin with.,
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Yes. I was using Visual Studio 2005.

    So it means that exception specifications now not in standard C++ and we should not use them?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Should is a strong word. Probably better to say their usage is discouraged.

    Deprecated formally means identified in the standard as slated for removal from a future version of the standard. It is a fairly strong hint that the feature will go away.

    Even if exception specifications remained standard, I would rarely (if ever) use them. And, if you stick with using VS2005, there's little point anyway.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Is it possible that this is happening because a char implicitly converts to an int?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elkvis View Post
    Is it possible that this is happening because a char implicitly converts to an int?
    I doubt it. main() has a catch(int) clause, which would catch the exception in that case. The program would not then exit abnormally.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's It Called When...
    By SMurf in forum General Discussions
    Replies: 9
    Last Post: 06-24-2013, 10:22 PM
  2. Replies: 1
    Last Post: 05-07-2012, 11:39 PM
  3. Why the destructor is called?
    By scherzo in forum C++ Programming
    Replies: 5
    Last Post: 10-23-2007, 05:11 AM
  4. Don't know what it's called
    By toonlover in forum Windows Programming
    Replies: 5
    Last Post: 08-02-2006, 09:08 PM
  5. What's it called?
    By SMurf in forum Tech Board
    Replies: 4
    Last Post: 11-28-2003, 06:40 AM