Handle exception that can happen in a class-construtor

When using a class that can throw an Exception in the __construct-method, should I then wrap the instantiation of the class ($foo = new Foo;) in a try / catch - block?

If you have some way of handling the exception, yes. Otherwise leave it to a global exception handler to render a generic error. If you don’t have a global exception handler yet, create one :slight_smile:

For me it is bad style to create a constructor which can fail.
In that case I add an init method to the class but the constructor should always return an instance of the class even if that instance does not contain useful data.

I see your point but there are certain cases where I’d want to know up front. For example a logger class that logs to a file ID l’d want to know if the file it’s supposed to write exists and is writable. If it’s not there is no point in having that logger.

But in general, yes, constructors should not do stuff.