Exception Handling – Preventing System Crashes

exception handlingexceptions

Second of all, I was wondering if anyone knew what the difference was between exceptions (in the realm of exception control flow) and Exceptions (such as used in Java).

But are they there to basically protect the system from crashing by terminating the user program?

Best Answer

Exceptions exist to allow Exception Handling, which can avoid crashes but more generally prevent unwanted or unpredictable system behavior. For instance if my program's connection to a database times out it's usually not going to crash the system, but if I was depending on data from the database an exception can allow me to treat this data-less situation differently than normal.

Say by default my program displays a page of data based on what was returned from the database--well crap, I have no data. Instead of presenting a messed up view or continuing a potentially invalid operation I can catch this exception and fall back to a different database, read from local data, ask the user for data or otherwise return the user or system to a safe state (presumably one that will not immediately cause the same exception!)

In addition in systems where user input could be the cause/solution to a problem, exceptions can let a user know detailed and helpful info about the problem. Instead of the too common "An unhandled exception occurred at..." or "Intimidating Error Message Straight from SQL" you can tell the user something helpful or at least understandable like "Could not connect to resource B."

Related Topic