Throw an exception when object is invalid

Fix #41