Runtime Errors

A runtime error is an error that occurs during application execution. These errors are not predictable and occur due to programming errors that are not visible at compile time. An example of a runtime error is creating an application and giving users the ability to specify a filename, but then the file is not found on disk; another example is needing to access a database and passing an incorrect SQL query string. Obviously, in real-life applications, you should predict such possibilities and implement the appropriate error-handling routines (discussed in Chapter 6), but for the purpose of learning about the debugger, you need some code that voluntarily causes an error. If you continue with the debugging you began in the previous section, the application’s execution resumption causes a runtime error because the code is searching for a file that does not exist. When the error is raised, Visual Studio breaks the execution, as shown in Figure 2.34.

Image

FIGURE 2.34 The Visual Studio debugger encountering a runtime error.

As you can see, the line of code that caused the error appears highlighted. You can also see a pop-up window that shows some information about the error. In this case, the code searched for a file that does not exist, so a FileNotFoundException error was thrown and was not handled by error-handling routines; therefore, the execution of the application was broken. Visual Studio also shows a description of the error message. (In this example, it communicates that the code could not find the FakeFile.txt file.) Visual Studio also shows some suggestions. For example, the Troubleshooting Tips section suggest some tasks you could perform at this point, such as verifying that the file exists in the specified location, checking the pathname, and getting general help about the error. If you click a tip, you are redirected to the MSDN documentation about the error. This can be useful when you don’t exactly know what an error message means.

There are also options in the Actions group of the error message. The most important is View Detail, which enables you to open the View Detail window, shown in Figure 2.35. Notice how the StackTrace item shows the hierarchy of calls to classes and methods that effectively produced the error. Another interesting item is InnerException. In our example it is set to Nothing, but it’s not unusual for this item to show an exceptions tree that enables you to better understand what actually caused an error. For example, think of working with data. You might want to connect to SQL Server and fetch data from a database. If you do not have sufficient rights to access the database, the Runtime might return a data access exception that does not allow you to immediately understand what the problem is. Browsing the InnerException can help you understand that the problem was caused by insufficient rights. You need to go back to the code to fix it, and this is where the Edit and Continue features, discussed next, come in.

Image

FIGURE 2.35 The View Detail window enables you to examine what caused an exception.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.144.170