Thursday, January 24, 2019

Ensure that you write code to handle all types of error conditions

Here’s the bottom line when it comes to working with errors.

  • Always expect that errors can occur in a program.
  • Ensure that you write code to handle all types of error conditions.


30. Logging in Programs

One of the most important concept in programming, is logging of what is happening in the program. Logging helps one to understand how the program is behaving and whether it’s performing as per its requirements. In the previous section, we looked at error handling in programs. In order to ensure that these errors are recorded somewhere, we need to have a facility to log the errors. Programming languages provide a few different methods that enable us to log data. We’ll look at some of them next.

30.1 Debugging During Development

Some programming languages, such as C#, provide classes that can be used to write debug statements to the console of the Integrated Development Environment as the program runs. This helps give the developer a good insight as to how the program is running. Values, and how they flow in the program, can be seen via these debug statements. For example, the following snippet of code shows how to write a debug statement that outputs to the output window in Visual Studio.