Exception Handling and Debugging

No matter how good a programmer is, he will always encounter errors or bugs. Testing for errors eats up a large percentage of program development. It would be better if there are ways to prevent this errors or at least “degrade gracefully”, that is, inform the user about certain errors instead of the program suddenly crashing and showing up cryptic error messages the user can’t understand.

Thankfully, C# has ways to do just that. .NET Framework offers a large collection of Exception classes for specific errors. Exceptions are .NET’s way of saying an error was found while the program is running. The .NET Framework contains a large collection of exception classes that you can use to handle different errors on different situations. You can even create your own exception class! Exceptions are “thrown” by the program, and you need to “catch” these exceptions and act accordingly.

For example, in the computer world, an integer can never be divided by 0. Try that on a calculator and you will receive an error. If a C# program encounters such an error, it will throw a DivideByZeroException which is an exception that is thrown when you divide a number by zero.

Bugs are another term for errors or code that produce unwanted behavior to a program. Debugging is the process of removing those bugs, which means, removing the errors and misbehaviors. Visual Studio has tools for debugging, which finds errors and allows you to fix them. You will learn how to effectively use these tools to remove bugs(errors) from your program. Excessive testing and debugging of programs are necessary before you deploy the final version of your program.