Error Handling and Debugging in VB NET Types


Error Handling and Debugging in VB. NET Types of Error Programming errors are generally broken down into three types: Design-time, Runtime, and Logic errors. 2

Types of Error n 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 blue wiggly line pointing them out. If you try to run the programme, you'll get a dialogue box popping up telling you that there were Build errors. n Runtime errors are a lot harder to track down. As their name suggests, these errors occur when the programme is running. They happen when your programme 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 programme to crash. You should write code to trap runtime errors. n Logic errors also occur when the programme 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 programme. But they will ensure that it doesn't work properly. 3

Runtime errors in VB. NET n Runtime errors are a lot harder than Design Time errors to track down. As their name suggests, these errors occur when the programme is running. Runtime errors are the ones that crash your programme. A simple way to crash a programme is to divided by zero: Dim Num 1 As Integer Dim Num 2 As Integer Num 1 = 10 Num 2 = 0 Text. Box 1. Text = CInt(Num 1 / Num 2) n The CInt( ) part means Convert to an Integer. We're just making sure to convert the answer to the sum into a number. But run your programme and test it out. Click your button and see what happens. 4

Runtime errors in VB. NET n From the controls toolbox, add a Rich. Text. Box control to your form. Change the Name property of your Rich. Text. Box to rt 1. A Rich. Text. Box is just like a normal textbox but with more functionality. One of these extra functions is the ability to load a file directly. Delete or comment out any code you have for your button, and add the following line: rt 1. Load. File("C: test 10. txt", Rich. Text. Box. Stream. Type. Plain. Text) n All the line does is to load (or try to) the text file called "test 10. txt" into the Rich. Text. Box. The second argument just specifies that the type of file we want to load is a Plain Text file. n Run your programme, and then click the button. If you don't have a text file called "test 10. txt" in the root folder of your C drive, you'll get a Runtime error message n 5

Try … Catch in VB. NET has a inbuilt 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. n 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: Try Catch ex As Exception End Try n 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. 6

Try … Catch in VB. NET Try rt 1. Load. File("C: test 10. txt", Rich. Text. Box. Stream. Type. Plain. Text) Catch ex As Exception Msg. Box(ex. Message) End Try n When you run your programme, 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. n Because ex is an object variable, it now has its own Properties and methods. One of these is the Message property. Run your programme and test it out. Click your button. 7

Try … Catch in VB. NET Try Catch ex As Exception Finally End Try 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. 8

Logic Errors in VB. NET n Add a button to the form and try this code as an example of a logic error: Dim x As Integer Dim y As Integer Dim answer As Integer x = 10. 5 y=3 answer = x * y Text. Box 1. Text = answer n What is the error? ? 9

Breakpoints and Debugging Tools n To help you find out what went wrong, there is a tool in VB. NET called a Breakpoint n A breakpoint is like a note to VB. NET to stop your programme at a particular place. You add one to your code by clicking in the margins. A brown circled then appears, indicating where the code will break. 10

Breakpoints and Debugging Tools n Run your programme, and click the button. You are immediately returned to the coding window. The place where you put the Breakpoint will now have a yellow arrow on top of the brown circle. The brown highlighted line will now be yellow: 11

Breakpoints and Debugging Tools n The yellow highlight indicates where in your code VB. NET is. To continue checking your code, press F 10 on your keyboard. The yellow arrow, and the yellow highlight, jump down one line. The next line in your code will be highlighted: 12

Breakpoints and Debugging Tools n Then hold you mouse on the letter variable (e. g. variable x). The value this variable currently holds will be displayed: n You can see that x variable has not the value you expected to have (the value is 10 instead of 10. 5) 13

Breakpoints and Debugging Tools n You can use another debugging tool - the Locals window. n While your programme is still in Debug mode (the yellow line will still be there, if it is), click Debug > Windows > Locals from the menu bar. You should see the following in the bottom left of your screen: Locals means "Local variables". That is, variables declared in this section of the code. n 14

Example We have written the code below to calculate the average of all the integers from num 1 to num 2 (including num 1 and num 2). These two numbers will be given by the user in textboxes. Dim i, sum, num 1, num 2, count As Integer Dim average As Double Dim msg As String sum=0 num 1 = CInt(Text. Box 1. Text) num 2 = CInt(Text. Box 2. Text) For i = num 1 To num 2 sum = sum + i Next count = num 2 - num 1 average = sum / count msg = "The average is: " + average. To. String Message. Box. Show(msg) n n Handle the runtime errors that may occur during the execution of the above code. What is the logical error? Try to find it using breakpoints. 15
- Slides: 15