added exception handling to __toString() method