Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Throwing exceptions from a constructor is not only a perfectly legitimate pattern to use. It is also necessary to correctly implement Resource Acquisition Is Initialization (RAII) for some classes (this appears to be the case in your code). And trust me, you really do want RAII :)

However, please be aware of the caveat that the destructor of the object will not be executed if the constructor throws. But the individual members of the class will be destructed.

On a related note, it is also okay for the constructor to perform work if the work is necessary for RAII. Again under the caveat that you adhere to Single Responsibility Principle (SRP) and Dependency Injection (DI). See this question: http://stackoverflow.com/questions/7048515/is-doing-a-lot-in-constructors-bad https://stackoverflow.com/questions/7048515/is-doing-a-lot-in-constructors-bad

Throwing exceptions from a constructor is not only a perfectly legitimate pattern to use. It is also necessary to correctly implement Resource Acquisition Is Initialization (RAII) for some classes (this appears to be the case in your code). And trust me, you really do want RAII :)

However, please be aware of the caveat that the destructor of the object will not be executed if the constructor throws. But the individual members of the class will be destructed.

On a related note, it is also okay for the constructor to perform work if the work is necessary for RAII. Again under the caveat that you adhere to Single Responsibility Principle (SRP) and Dependency Injection (DI). See this question: http://stackoverflow.com/questions/7048515/is-doing-a-lot-in-constructors-bad

Throwing exceptions from a constructor is not only a perfectly legitimate pattern to use. It is also necessary to correctly implement Resource Acquisition Is Initialization (RAII) for some classes (this appears to be the case in your code). And trust me, you really do want RAII :)

However, please be aware of the caveat that the destructor of the object will not be executed if the constructor throws. But the individual members of the class will be destructed.

On a related note, it is also okay for the constructor to perform work if the work is necessary for RAII. Again under the caveat that you adhere to Single Responsibility Principle (SRP) and Dependency Injection (DI). See this question: https://stackoverflow.com/questions/7048515/is-doing-a-lot-in-constructors-bad

Source Link
Emily L.
  • 16.7k
  • 1
  • 39
  • 89

Throwing exceptions from a constructor is not only a perfectly legitimate pattern to use. It is also necessary to correctly implement Resource Acquisition Is Initialization (RAII) for some classes (this appears to be the case in your code). And trust me, you really do want RAII :)

However, please be aware of the caveat that the destructor of the object will not be executed if the constructor throws. But the individual members of the class will be destructed.

On a related note, it is also okay for the constructor to perform work if the work is necessary for RAII. Again under the caveat that you adhere to Single Responsibility Principle (SRP) and Dependency Injection (DI). See this question: http://stackoverflow.com/questions/7048515/is-doing-a-lot-in-constructors-bad

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /