Refactoring `write!` and `close!` to use a new-style exception if the
handle is closed.

This also fixes a bug with `write!`, as it didn't previously check that
the handle was closed.