set_unexpected and set_terminate

This is a discussion on set_unexpected and set_terminate within the C++ Programming forums, part of the General Programming Boards category; I want to use my own functions instead of the default unexpected() and terminate() Could any one give me a ...

  1. #1
    Unregistered
    Guest

    set_unexpected and set_terminate

    I want to use my own functions instead of the default unexpected() and terminate() Could any one give me a short example on how to declare set_unexpected and set_terminate in main for this? I can't seem to get it to work. Thanks

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Something like -

    Code:
    #include <iostream>
    #include <exception>
    
    
    using namespace std;
    
    void my_terminate()
    {
        cout << "my terminate";
        cin.get();
        exit(0);
    }
    
    
    void fn()
    {
    	throw;
    }
    
    
    int main()
    {
    	
    	set_terminate(my_terminate);
    	fn();
            return 0;
    }

  3. #3
    Unregistered
    Guest
    thanks, what about set_unexpected though? I want it to go through my unexpected function when I throw an unexpected object then my_unexpected calls my_terminate.

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    This works on Dev C++ (mingw) -

    Code:
    #include <iostream>
    #include <exception>
    using namespace std;
    
    void my_unexpected()
    {
    	cout << "my unexpected";
        throw;
    	
    }
    
    void my_terminate()
    {
        cout << "my terminate";
        cin.get();
        exit(0);
    }
    
    
    void fn()throw()
    {
       throw unexpected;
    	
    }
    
    
    int main()
    {
    	
    	set_unexpected(my_unexpected);
        set_terminate(my_terminate); 
    	fn();
    	return 0;
    }

  5. #5
    Unregistered
    Guest
    hmmm, I was doing something very similar, but for some reason when I throw an unexpected object it just goes straight to terminate() and not to unexpected(). I'm running my programs in UNIX

    Thanks, anyway

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21