title: Throwing exceptions from constructors
body: <p>I'm having a debate with a co-worker about throwing exceptions from constructors, and thought I would like some feedback.</p>
  
  <p>Is it OK to throw exceptions from constructors, from a design point of view?</p>
  
  <p>Lets say I'm wrapping a POSIX mutex in a class, it would look something like this:</p>
  
  <pre><code>class Mutex {
  public:
    Mutex() {
      if (pthread_mutex_init(&amp;mutex_, 0) != 0) {
        throw MutexInitException();
      }
    }
  
    ~Mutex() {
      pthread_mutex_destroy(&amp;mutex_);
    }
  
    void lock() {
      if (pthread_mutex_lock(&amp;mutex_) != 0) {
        throw MutexLockException();
      }
    }
  
    void unlock() {
      if (pthread_mutex_unlock(&amp;mutex_) != 0) {
        throw MutexUnlockException();
      }
    }
  
  private:
    pthread_mutex_t mutex_;
  };
  </code></pre>
  
  <p>My question is, is this the standard way to do it? Because if the <code>pthread mutex_init</code> call fails the mutex object is unusable so throwing an exception ensures that the mutex won't be created.</p>
  
  <p>Should I rather create a member function init for the Mutex class and call <code>pthread mutex_init</code> within which would return a bool based on <code>pthread mutex_init</code>'s return? This way I don't have to use exceptions for such a low level object.</p>