Hacker News new | ask | show | jobs
by IvyMike 4669 days ago
In C++, use exceptions when things go so badly that you have to metaphorically pack things up and send some people home. Because that's what exceptions are good at.

For example, I used to work on a system where there were various threads each controlling a different piece of hardware. But the hardware was hot-swapable, so it was possible that suddenly a thread would find itself trying to talk to hardware that was no longer there. We would throw an exception at the low level hardware access point, at catch at the (nearly) highest level of the thread. This allowed us to reclaim memory and other resources used by the thread in an orderly fashion, and return to a "ready for hardware insert" mode. This was key since the other threads were doing just fine and the overall program needed to continue.