Download presentation
Presentation is loading. Please wait.
Published byBernard Richard Modified over 9 years ago
1
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης
2
2 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.
3
3 Types of Error 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. 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. 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.
4
4 Runtime errors in VB.NET 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 Num1 As Integer Dim Num2 As Integer Num1 = 10 Num2 = 0 TextBox1.Text = CInt(Num1 / Num2) 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.
5
5 Runtime errors in VB.NET From the controls toolbox, add a RichTextBox control to your form. Change the Name property of your RichTextBox to rt1. A RichTextBox 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: rt1.LoadFile("C:\test10.txt", RichTextBoxStreamType.PlainText) All the line does is to load (or try to) the text file called "test10.txt" into the RichTextBox. The second argument just specifies that the type of file we want to load is a Plain Text file. Run your programme, and then click the button. If you don't have a text file called "test10.txt" in the root folder of your C drive, you'll get a Runtime error message
6
6 Try … Catch in VB.NET 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. 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 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.
7
7 Try … Catch in VB.NET Try rt1.LoadFile("C:\test10.txt",RichTextBoxStreamType.PlainText) Catch ex As Exception MsgBox(ex.Message) End Try 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. 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.
8
8 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.
9
9 Logic Errors in VB.NET 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 TextBox1.Text = answer What is the error??
10
10 Breakpoints and Debugging Tools To help you find out what went wrong, there is a tool in VB.NET called a Breakpoint 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.
11
11 Breakpoints and Debugging Tools 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:
12
12 Breakpoints and Debugging Tools The yellow highlight indicates where in your code VB.NET is. To continue checking your code, press F10 on your keyboard. The yellow arrow, and the yellow highlight, jump down one line. The next line in your code will be highlighted:
13
13 Breakpoints and Debugging Tools Then hold you mouse on the letter variable (e.g. variable x). The value this variable currently holds will be displayed: You can see that x variable has not the value you expected to have (the value is 10 instead of 10.5)
14
14 Breakpoints and Debugging Tools You can use another debugging tool - the Locals window. 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.
15
15 Example We have written the code below to calculate the average of all the integers from num1 to num2 (including num1 and num2). These two numbers will be given by the user in textboxes. Dim i, sum, num1, num2, count As Integer Dim average As Double Dim msg As String sum=0 num1 = CInt(TextBox1.Text) num2 = CInt(TextBox2.Text) For i = num1 To num2 sum = sum + i Next count = num2 - num1 average = sum / count msg = "The average is: " + average.ToString MessageBox.Show(msg) 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.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.