The Debug Class
- Remove the breakpoints
- Double-click an empty space on the form and create form_load event
- Add the following code
Debug.Write("Form Loading")
Use this to print out debugging information as your program runs in the output window
- Open the output window by selecting View from the menu and Other windows and then Output
Your message displays at the bottom of the output window
Error Handling
Errors created by bugs or events in your code need to be handled in code
Try-Catch
Try a piece of code
If a problem develops an error is thrown
You then catch that error
Type TRY
Visual Studio adds the rest
Try
Catch ex as exception
End try
Exceptions tell your code something is wrong
The catch statement catches the exception object
Exception object inherits from System.exception
Example:
- Add a new windows form
- Double-click myproject and change the startup form to form2.vb
Add this code to the page_load by double-clicking the form
Try
Dim bigError As Integer = String.Empty
Catch ex As Exception
Debug.Write("error:" + ex.Message)
End Try
Notice the message in the output window:
You can look at the contents of the exception object by typing
?ex in the immediate window after setting a breakpoint next to the debug.write code
Step through until you get past the (catch ex as exception) statement
Message property has the error that occurred
Stacktrace property has the line number the error occurred
Type ?ex.message
Type ?ex.stacktrace
Here are all the properties from
http://msdn2.microsoft.com/en-us/library/system.exception_members.aspx
Name |
Description |
Data |
Gets a collection of key/value pairs that provide additional, user-defined information about the exception. |
HelpLink |
Gets or sets a link to the help file associated with this exception. |
InnerException |
Gets the Exception instance that caused the current exception. |
Message |
Gets a message that describes the current exception. |
Source |
Gets or sets the name of the application or the object that causes the error. |
StackTrace |
Gets a string representation of the frames on the call stack at the time the current exception was thrown. |
TargetSite |
Gets the method that throws the current exception. |
Debugging example:
Press F10 to start debugging
Continue to step through the code by pressing F10
Step over a method by pressing F10
Step into a method by pressing F11
To continue in run mode press F3
- Start a new windows project
- Drag 2 textboxes, a label and a button to your form
- Add code to the button to divide the first textbox value by the second textboxes value
Dim num1 As Double = Double.Parse(TextBox1.Text)
Dim num2 As Double = Double.Parse(TextBox2.Text)
Label1.Text = (num1 / num2).ToString()
Double.parse converts the text to a double
This would be the same as
Ctype(textbox1.text,double)
Run the program and enter 2 values
Now type a number in the first textbox and the word two into the second textbox
Now you get an error because double.parse expects a string with a numeric value
Click link that says view details
Change the code so you can capture the error
Try
Dim num1 As Double = Double.Parse(TextBox1.Text)
Dim num2 As Double = Double.Parse(TextBox2.Text)
Label1.Text = (num1 / num2).ToString()
Catch ex As Exception
MessageBox.Show("oops")
End Try
Or you can show the error by changing the mesasgebox code to this
You can catch specific exception
Try
Dim num1 as double=double.parse(textbox1.text)
Dim num2 as double=double.parse(textbox2.text)
Label1.text=(num1/num2).toString()
Catch formatEx as formatException
Messagebox.show(“Enter a number”)
Catch ex as exception
Messagebox.show(“Had an error:” + ex.message)
End try
Pick from a list of exceptions after you type AS or type system. And pick from that list
Finally
A finally block will run even if there was an exception
Try
Dim num1 As Double = Double.Parse(TextBox1.Text)
Dim num2 As Double = Double.Parse(TextBox2.Text)
Label1.Text = (num1 / num2).ToString()
Catch ex As Exception
MessageBox.Show("oops")
Finally
MessageBox.Show("This executes no matter what")
End Try
Conditional Catches
Only supported in Visual Basic (not C#)
You can create catch blocks that only run when a certain exception type occurs and some other condition is true
Try
Catch ex as exception when num2=0
End try
Try
Runmethod()
Catch ex as exception
Debug.write(“Error”)
End try
Public sub runmethod()
‘some code here
end sub
if an error occurs in the method it bubbles up to the try block that called the method
Throw Exceptions
If not booleanValue then
Threw new exception(“Your method failed”)
End if