To start off, I'll automatically assume you know the basics of VB, such as how to use the form designer and access the coding area and developing events and methods for your controls.
What is error handling?
Error handling is a system that you should really implement in all your programs to efficiently deal with errors that might show up. This is so that you can easily fix the error without having your application crash, or for letting the user know that they did something wrong and can go back to what they were doing without having the application crashing either.
How does it work?
Well in VB.net there are a few ways to handle errors. One of them is if you know specifically what will go on, the other is old and inefficient (in my opinion) and finally the last one is modern, efficient and very easy to understand.
Method #1
The first method is to check input or anything else that can trigger an error and analyze it for the possible issue that you think could happen. A good example is if you have a project that requires numerical input, but the user enters a string into a textbox instead of a numerical value. This can be checked with the following:
If IsNumeric(TextBox1.Text) = False Then
Msgbox("Value in TextBox1 is not numerical.")
Exit Sub
End If
As you can see, this method only works if you know what can happen.
The IsNumeric() method is extremely useful as it returns a boolean judging on the string it is given to check if it is a numerical value or not. It will return false if it is not and true if it is.
Once you've checked for the error, you can display what's wrong (as shown above) or place in some other code to automatically fix the issue.
Method #2
The second method was the way to handle errors in VB6, but is still used on occasion in VB.net. This is the common "On Error GoTo ErrorHandler" or "On Error Resume Next" statements.
Here is an example of an "On Error GoTo" handler:
On Error GoTo Err
Err:
msgbox(err.description)
That code, in essence, tells the application to go to a specific area of the code to execute the handler once an error is thrown. Here you can see that there is a class used that you might not be familiar with; that is the err class. This class makes error handling and display very easy for developers as it pretty much does all the work for you. There are various methods in the class, but the most common ones are "err.description" and "err.number". The description method returns the error's description in the form of a string, so you can easily display it in a message box or something. The number method returns the error's ID number for more specifics.
Here is an example of an "On Error Resume Next" handler:
Yep, that's it, short and sweet. This is frowned upon by many developers since all it does is ignores the error completely. So, if the error is fatal, the application will crash without any explanations what-so-ever. It's okay to use if the error is small and insignificant, but is not suggested for use with all errors.
Method #3
This method is the best and most comonly used way to deal with errors. It's used in most other languages, primarily the C family. It is called a Try/Catch block. The way it works is that the code tells the application to try out a block of code and if an error is thrown, to "catch" it as an exception then to execute the handler code. A small example of a Try/Catch block would be the following:
Try
Dim str As String = "Two"
Dim int as Integer = str/2
Catch Ex As Exception
Msgbox(err.description)
End Try
As you can see, you need to begin the block with the "Try" statement. This initializes the portion of code to try and do. This portion is ended by declaring the exception (or the error) so we can define it and display it. The default exception name in VB is "Ex", but you can name it whatever you like. If this is also too generic and you want to find a specific exception, you can change type "Exception" to the name of the exception you want to catch, for example "InvalidOperationException".
After the exception declaration, you put in your handler code. Here I used the err class once again and displayed the error's description in a message box.
Once you're all done, you can end the Try/Catch block with a traditional "End" class.
Consider your errors now handled