Is it ever not safe to throw an exception in a constructor?

前端 未结 5 1767
长情又很酷
长情又很酷 2020-12-05 03:46

I know that it\'s not safe to throw exceptions from destructors, but is it ever unsafe to throw exceptions from constructors?

e.g. what happens for objects that ar

5条回答
  •  时光取名叫无心
    2020-12-05 04:20

    If you take a resource in a constructor, such as a socket etc, then this would be leaked if you threw an exception would it not?

    But I guess this is the argument for doing no work in a constructor, lazy initializing your connections as you need them.

提交回复
热议问题