Identifying the Two Basic Types of Errors


There are basically two types of errors that can occur in code: compile errors and runtime errors. A compile error (commonly called a build error) is code that prevents Visual C#'s compiler from being able to process the code; Visual C# won't compile a project that has a build error in it. A statement that calls a procedure with incorrect parameters, for example, generates a build error. Runtime errors are those that don't occur at compile time but are encountered when the project is being run. Runtime errors are usually a result of trying to perform an invalid operation on a variable.

To illustrate, consider this next statement which won't generate a compile error:

intResult = 10 / intDenominator;


Under most circumstances, this code won't even generate a runtime error. However, what happens if the value of intDenominator is 0? Ten divided by zero is undefined, which won't fit into intResult (intResult is an int variable). Attempting to run the code with the intDenominator variable having a value of 0 causes Visual C# to return a runtime error. A runtime error is called an exception, and when an exception occurs, it's said to be thrown (that is, Visual C# throws an exception when a runtime error occurs). When an exception is thrown, code execution stops at the offending statement and Visual C# displays an error message. You can prevent Visual C# from stopping execution when an exception is thrown by writing special code to handle the exception, which you'll learn about later in this hour.

Add the following statements to your Click() event now, right below the two comment lines:

long lngAnswer; lngAnswer = 100 / Convert.ToInt64(txtInput.Text); MessageBox.Show("100/" + txtInput.Text + " is " + lngAnswer)


The missing semicolon in the MessageBox.Show() line is intentional; type in the preceding line of code exactly as it appears. Although you've missed the ending semicolon, Visual C# doesn't return an immediate error. However, notice how Visual C# displays a wavy red line at the end of the statement. Note that you may have to press Enter to move to a different line to get it to show up. Choose Error List from the View menu now, and notice that Visual C# displays an information tip explaining the nature of the error (see Figure 15.2). All build errors in the current project appear in the Error List. To view a particular offending line of code, double-click an item in the Error List.

Figure 15.2. Visual C# highlights build errors in the code window by using wavy lines.


Press F5 to run the project. When you do, Visual C# displays a message that a build error was found and asks whether you want to continue by running the last successful build. Because the code won't run as is, there's no point in continuing, so click No to return to the code editor.

Build errors are very serious in that they prevent code from being compiled and therefore they completely prevent execution; build errors must be corrected before you can run the project. Double-click the build error in the Error List to go directly to the error now.

Correct the problem by adding a semicolon to the end of the MessageBox.Show() statement. After you've made this change, press F5 to run the project. Visual C# no longer returns a build error; you've just successfully debugged a problem!

Click the Perform Division button now, and you'll receive another error (see Figure 15.3).

Figure 15.3. A runtime exception halts code execution at the offending line.


This time, the error is a runtime error, or exception. If an exception occurs, you know that the code compiled without a problem because build errors prevent code from compiling and executing. This particular exception is a Format exception. Format exceptions generally occur when you attempt to perform a method using a variable, and the variable is of an incompatible data type for the specified operation. Visual C# denotes the offending statement with a yellow arrow (the arrow indicates the current statement).

At this point, you know that the statement has a "bug," and you know it is related to data typing. Choose Stop Debugging from the Debug menu to stop the running project and return to the code editor.




Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net