>>21
Write an error message to stderr, of course. The most likely cause of a write to stdout failing is a disk filling up when stdout is redirected to a file on said disk. If stderr is redirected somewhere else, your chances of recovery are much better.