Types+of+Errors

Programming errors are generally broken down into three types: **Design-time**, **Logic** **Runtime**, and errors. A **Design-time error** is also known as a syntax error. These occur when the environment you're programming in doesn't understand your code. These are easy to track down in VB.NET, because you get a wiggly line pointing them out. If you try to run the program, you'll get a dialogue box popping up telling you that there were “Build” errors. ** Logic errors ** also occur when the program is running. They happen when your code doesn't quite behave the way you thought it would. A classic example is creating an infinite loop of the type "Do While x is greater than 10". If x is always going to be greater than 10, then the loop has no way to exit, and just keeps going round and round. Logic errors tend not to crash your program. But they will ensure that it doesn't work properly. ** Runtime errors ** are a lot harder to track down. As their name suggests, these errors occur when the program is running. They happen when your program tries to do something it shouldn't be doing. An example is trying to access a file that doesn't exist. Runtime errors usually cause your program to crash. If and when that happens, you get the blame. After all, you're the programr, and you should write code to trap runtime errors. If you're trying to open a database in a specific location, and the database has been moved, a Runtime error will occur. It's your job to predict a thing like this, and code accordingly. VB.NET has a built-in class that deals with errors. The Class is called **Exception**. When an exception error is found, an **Exception object** is created. The coding structure VB.NET uses to deal with such Exceptions is called the Try … Catch structure. In the coding area for your button, type the word **Try**. Then hit the return key on your keyboard. VB.NET completes the rest of the structure for you: The Try word means "Try to execute this code". The Catch word means "Catch any errors here". The ex is a variable, and the type of variable it is is an Exception object. Move your line of code from the [|previous section] to the Try part: When you run your program, VB will ** // __Try__ // ** to execute any code in the Try part. If everything goes well, then it skips the Catch part. However, if an error occurs, VB.NET jumps straight to Catch. Add the following to your Catch part: Your coding window should look like this: Because ex is an object variable, it now has its own Properties and methods. One of these is the Message property. Run your program and test it out. Click your button. You should see the following error message: The message is coming from the "additional Information" section of the error message we saw earlier, the one we didn't handle. But the point about this new message box is that it will not crash your program. You have handled the Exception, and displayed an appropriate message for the user. If you know the kind of error that a program might throw, you can get what Type it is from the Error message box you saw earlier. This one: Click the **View Details** links under **Actions** to see the following: The first line tells us the Type of Exception it is: ** System.IO.FileNotFoundException ** You can add this directly to the catch part. Previously, you were just catching any error that might be thrown: ** Catch ex As Exception ** But if you know a "file not found" error might be thrown, you can add that to the Catch line, instead of Exception: ** Catch ex As System.IO.FileNotFoundException ** You can keep the Exception line as well. (You can have as many Catch parts as you want.) This will Catch any other errors that may occur:  There is one last part of the Try … Catch Statement that VB.NET doesn't add for you and it is called **// __Finally__ //**: The **Finally** part is always executed, whether an error occurs or not. You typically add a Finally part to perform any cleanup operations that are needed. For example, you may have opened a file before going into a Try … Catch Statement. If an error occurs, the file will still be open. Whether an error occurs or not, you still need to close the file. You can do that in the **Finally** part.
 * __ “Catching” Run-Time Errors __ **
 * Try **
 * Catch ex As Exception **
 * End Try **
 * Try **
 * rt1.LoadFile("C:\test10.txt", RichTextBoxStreamType.PlainText) **
 * Catch ex As Exception **
 * End Try **
 * MsgBox(ex.Message) **
 * Try **
 * rt1.LoadFile("C:\test10.txt", RichTextBoxStreamType.PlainText) **
 * Catch ex As System.IO.FileNotFoundException **
 * MsgBox(ex.Message) **
 * Catch ex As Exception **
 * MsgBox(ex.Message) **
 * End Try **
 * Try **
 * Catch ex As Exception **
 * Finally **
 * End Try **