Introduction This article talks about different ways of handling Abnormal Condtions or Errors that occur or Invalid Data receiving by an Application. These Errors are something, the programmer expect to occur in their Applications. Depending on the specific circumstances, below are some tactics, using which we can handle errors. These techniques have to be handled both in Production and Development code as well. These techniques are used by the programmer to handle errors gracefully. Background Difference between Asserts and Error Handling Techniques: Before knowing about different techniques, we must know about when to use Asserts and when to use Error Handling Techniques. Assertion are used to handle errors that should never occurs in the code. If there is an Assert, then we must think there is a bug in the Application and must be fixed. Asserts are used to handle inputs that are coming from inside the same Application. Error handling is handing An error that you expect to occur. This is used to handle inputs that are coming from outside or external Application, which we can not predict. Here are some Techniques to Handle Errors. Return a neutral value Sometimes instead of Asserting, the best response to bad external data is to continue operating and simply return a neutral value that's known to be harmless. For Example: A numeric computation might return O A string operation might return an empty string a pointer operation might return an empty pointer In an video game, a drawing routine that gets a bad input value for color, might use the default background or foreground color Substitute the next piece of valid data On finding a currupted or invalid record, while processing a stream of data, developer can continue processing until he finds a next valid record. For Example: If an Application, taking readings from a thermometer 100 times per second and if it does't get a valid reading one time, it might simply wait for another 1/100th of a second and take the next reading. Return the same answer as the previous time On finding an invalid data in processing, instead of using the same, an Application can use the previous valid data and do the processing. For Example: In a video game Application , if it finds a request to paint a part of the screen with an invalid color, it might simply use the same color that was used previously. Substitute the closest valid value Sometimes, an Application can use the closest Valid Value to that of received invalid data as input. For Example: If an Application expects a Data within a Range 0-100 and if it receives "-1" as input, then it can simply take the closest value to -1 as "0" and continue processing. Log a warning message/software errors to a file On detecting a bad data while processing, an Application might simply log a Warning or Software Error to a log file and continue processing with next valid data. For Example: If an Application expects a non-zero and if it receives a "Zero", then it can simply log a Warning message like: "Receving "o" from xxx Message/Event" to a log file. Return an error code If in an Application, certain part of it kept only to Handle Errors and all other parts of it will not handle errors and will report that an error has been detected. So the specific way to report the rest of the system that an error has occurred could be any of the following: Set the value of a status (output variable) variable Return status as the function's return value Throw an exception by using the language's built-in exception mechanism Then the called system or relevant system must check the status of the returned variable and take necessary actions. Display an error message wherever an error is encountered This approach minimizes the error-handling overhead; however, it does have the potential to spread user interface messages through the entire application, which inturn can create challenges for creating a consistent user interface, when you try to clearly separate the UI from the rest of the system. Call an error-processing routine/object This is to centralize error handling in a global error-handling routine or error handling object. The advantage of this approach is that error-processing responsibility can be centralized, which can make debugging easier. Shutdown Some systems shut down whenever they detect an error or invalid data. This approach is useful in safety-critical applications. For Example: If an Application is written to control radi*ation equipment for treating cancer patients and if it receives bad input data for the radiation dosage, then the best option to handle such error is to simply Shutdown the Application than to take the risk of delivering the wrong dosage. BottomLine: External classes in an Application should use Error-Handling as it is not safe to make any Assumptions on the incoming external data, whereas Internal classes should use Assertions, as input data is coming from the inside classes only and so there must be a Wrong Assuption or Error in the Application itself than in the received data.