Exception handling is a common programming language construct that allows programs to identify and gracefully handle mistakes that they might encounter as they run. It works by changing the flow of a program from normal execution to a specific set of mitigating actions when a mistake is found. A programmer can create specific sets of actions that are invoked when certain mistakes happen. Finding and handling exceptions is not necessarily a fatal event for a program; it is sometimes possible for program execution to continue after exception handling occurs.
It is often possible, while programming a specific piece of code, for one to determine exactly where exception handling is necessary. In that case, most programming languages, including Java and C++, have specific keywords and control structures that can be placed into code to handle errors. These structures can handle a wide variety of errors, and with proper maintenance, it is often possible for a programmer to foresee the majority of possible errors for a particular piece of code.
Exception handling can also be useful for parsing input. For example, when parsing numeric input, one can immediately tell if a piece of data is non-numeric if the proper test and exception handler is set up. If an exception occurs, the handler catches it, which immediately tells the program that input is invalid. What happens next depends on how the rest of the program is structured; it might exit, or it might continue and simply ignore the invalid input.
Proper exception handling does not guarantee flawless program execution. It also doesn't guarantee that a program will stop after an error is found, and it doesn't guarantee that a program will continue. In order to handle errors properly, a programmer must use exception handling in conjunction with common sense and careful attention to detail. Ideally, proper error handling will allow a program to avoid the most serious types of errors, such as invalid memory accessing, while giving informative messages about fixable issues, such as invalid input or unexpected problems with a program's execution.
Exception handling catches errors called "run time" errors; it does not guarantee that code is free from bugs or syntax errors. Having error handling in code does not guarantee that the code is correct. It is possible for error handling to miss bugs in code, and even the best error catching is rarely foolproof. After debugging is complete, the presence of exception handling allows for much easier code maintenance and future debugging, if necessary.