Put this line inside the context manager. Also, I think you can probably do cm.exception.message or something rather than a somewhat ambiguous str cast.