An Exceptional Constructor

        Submitter: Oleg Goldshmidt

        Language: C++

        Date:        2003/07/16

Today I came across the following code:

#include <pthread.h>

class MutexException
{
protected:
int code;
public:
MutexException(int c) : code(c) {}
};

class ExceptionalMutex
{
protected:
pthread_mutex_t mtx;
public:
ExceptionalMutex();
};


ExceptionalMutex::ExceptionalMutex(void)
{
int rc = 0;
pthread_mutexattr_t attr;

if ((rc = pthread_mutexattr_init(&attr)) != 0)
throw MutexException(1);

if ((rc = pthread_mutex_init(&mtx,&attr)) != 0)
throw MutexException(2);

if ((rc = pthread_mutexattr_destroy(&attr)) != 0)
throw MutexException(3);
}

ExceptionalMutex global_mutex;

This is grossly simplified, of course, and in fact the code above was spread over at least 5 files (the two class declarations in headers, their implementations in source files, and the global mutex declaration in yet another one) in at least 2 directories. 

To spare those who have not yet reacted the suspense, global_mutex will be created before execution reaches  main(). Who do you think will catch the exception if one is thrown in the constructor?