Thread: std::bad_exception donīt throw exception

  1. #1
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343

    std::bad_exception donīt throw exception

    Hmmm, canīt really grasp the logic of std::bad_exception or I am simple using it wrong. Iīve tried to make a board seach on the topic but I wasnīt succesful. If I understand it correctly std::bad_exception is used when an exception is thrown that isnīt declared in the exception specification, e.i.

    Code:
    #include <iostream>
    #include <conio.h>
    #include <exception>
    
    using namespace std;
    
    class A
    {};
    class B
    {};
    
    void test() throw(A, std::bad_exception)
    {
        throw B();//Here should it throw a std::bad_exception type 
    //throw A(); //This works okey 
    }
    
    int main(void)
    {
    
        try
        {
            test();    
        }
        catch(A)
        {
            cout << "Error in CLASS A" << endl;
        }
        //Canīt catch it here
        catch(std::bad_exception)
        {
            cout << "Error in std::bad_exception" << endl;
        }
    
        getch();
    
    return 0;
    }
    When I run this code I get a "abnormal program termination". When I delved deeper in my book I found that that the line
    Code:
    throw B();//Here should it throw a std::bad_exception type
    actually doesnīt throw it directly but it goes through a function called unexpected() which calls another function, terminate(), that terminates the program (like in my case). Well if it terminates the program and you have no acces to the "supposed" thrown object, then whats the purpose of std::bad_exception??? Iīm totaly lost.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Exception specifiers look like a good idea, but the unhandled problem is a killer at times.....

    You can set your own unexpected handler if you wish and then rethrow whatever

    Code:
    #include <iostream>
    #include <exception>
    
    using namespace std;
    
    class A
    {};
    class B
    {};
    
    void catch_unexpected()
    {
    	cout << "Woops - caught a bad_exception" << endl;
    	throw;//rethrow bad_exception
    }	
    
    void test() throw(A, std::bad_exception)
    {
        throw B();//Here should it throw a std::bad_exception type
    }
    
    int main(void)
    {
    
    	unexpected_handler uh = set_unexpected(catch_unexpected);
        try
        {
            test();   
        }
        catch(A)
        {
            cout << "Error in CLASS A" << endl;
        }
        catch(std::bad_exception)
        {
            cout << "bad_excepetion caught" << endl;    
        }
        
        set_unexpected(uh);
    
    	cout << "Execution resumes" << endl;
    
    return 0;
    }

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    no versions of visual c support properly exception specifications other than the throws nothing spec throw(). Generally other than saying a function cant emit an exception there is not too much use for exception specifiers.Another bad idea from the people who brought you c++, to go along with the auto keyword, register keyword, vector<bool> and the absolutely useless export.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lifetime of temporary during exception throw
    By brewbuck in forum C++ Programming
    Replies: 3
    Last Post: 05-22-2009, 04:08 PM
  2. STL sort throw exception?
    By George2 in forum C++ Programming
    Replies: 11
    Last Post: 01-10-2008, 06:34 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 5
    Last Post: 06-06-2007, 11:10 PM
  5. Problem with the exception class in MINGW
    By indigo0086 in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2007, 01:12 PM