Try to catch locations where the program might fail if enough memory
is not available, and exit gracefully. Extended version of
improvements suggested by NP!