- Increased programming reliability – suppose you have an application that reads a file from the disk for processing, if the user selects a corrupt file or an invalid one, then an exception will be thrown. If you have an exception handling block, then you will be able to provide useful feedback to the users and provide them with an option to select a valid file
- Rollback incomplete transactions - exception handling is used to rollback transaction processing if an error occurs when executing SQL queries.
- Centralized error/exception processing – when an exception is thrown, you can have a dedicated exception handling class with a method that writes to the exception message to a log file for later analysis.
3 Reasons Why You Should Handle Exceptions in Your Code
No code is bug free or written to anticipate very possible scenario. The unexpected behavior may cause your application to stop working, corrupt data if it was in the process of updating multiple tables with the same values etc. Exception handling allows us to gracefully handle these unexpected behaviors. Exception handling is the process of responding to the occurrence, during computation, of exceptions – anomalous or exceptional events requiring special processing – often changing the normal flow of program execution. It is provided by specialized programming language constructs or computer hardware mechanisms. Below are the three (3) reasons why you should write exception handling code.