Welcome to the VB.NET tutorial series: "VB.NET from beginner to advanced programmer" which will take you from the very beginning to be a good programmer. VB.NET is a good first language for new programmers so this 21 part long series is written for completely beginners but it will also works perfectly fine if you already know another programming language.
VB.NET from beginner to advanced programmer
- Introduction and Installation
- Objects and Events
- Variables
- The basic data types
- Logical Operators
- Relational Operators
- If statements Then
- Arithmetical Operators
- Loops Part 1
- Arrays
- Loops Part 2
- Try Catch statements
- Subs and Functions
- Difference between Scopes
- Select Statements
- Multidimensional arrays
- Structures
- Classes
- Enumerations
- Advanced Comments
- Compiling Directives
Right now we've come to error handling which we'll do with something called the Try Catch statement. Try Catch statements are used to catch run-time errors to prevent errors to ruin your program. The most simple type of Try Catch statements could look like this:
So in the above example we tries to divide 10 with a variable called number in the Try block. If an error occurs somewhere in a try block the code will immediately go to the catch block and continue from there. So if number is 0 in this case an error will occur and we will see "Can't divide by 0". Observe that the above code is not something you should really do, catching an error takes relative long time so always use if statements if it's possible, like this:Code:Dim Number As Integer = 10 Try MessageBox.Show(10 / Number) Catch MessageBox.Show("Can't divide by 0") End Try
But it isn't always possible to use if statements to see if an error will occur and that's what we have the try catch statements for.Code:Dim Number As Integer = 10 If Number <> 0 Then MessageBox.Show(10 / Number) Else MessageBox.Show("Can't divide by 0") End If
We can also give a name to the exception:
So now the catchblock is named ex and is of the type exception. Then we just show ex.message if an error occurs which is the error message. Which in this case will be something like "The object reference haven't been given an instance of an object.", I don't know exactly since these messages is different depending on your computer's language and mine is Swedish.Code:Dim myVariable As Form Try myVariable.Show() Catch ex As Exception MessageBox.Show(ex.Message) End Try
The type Exception is the standard one and will catch all exception, however we may add as many catch block we want with a different exception type. Then if an error occurs it tries to find the first catch block that matches the error which occurred. If no catch blocks matched it would be exactly the same as without any try catch statement, so a good idea is to put a general exception at the end, a little example:
Code:Dim myVariable As Form Try myVariable.Show() Catch ex As NullReferenceException MessageBox.Show("myVariable can't be empty") Catch ex As Exception MessageBox.Show(ex.Message) End Try
You can also specify an condition for each catch block, the condition also needs to be true for the catch block to match with the error:
Code:Dim Friendly As Boolean = True Dim myVariable As Form Try myVariable.Show() Catch ex As NullReferenceException When Friendly = False MessageBox.Show("Are you stupid? myVariable can't be empty, noob!") Catch ex As NullReferenceException MessageBox.Show("myVariable can't be empty") Catch ex As Exception MessageBox.Show(ex.Message) End Try
In the small example below the first catch block won't be run since Friendly is not equals to False. Therefor the next one will be run instead.
At the end we can add something called finally, the code in here will always be executed in the end, there's no different if an error occurred or not:
And that was what I had to say about the Try catch statements. See you laterCode:Dim Friendly As Boolean = True Dim myVariable As Form Try myVariable.Show() Catch ex As NullReferenceException When Friendly = False MessageBox.Show("Are you stupid? myVariable can't be empty, noob!") Catch ex As NullReferenceException MessageBox.Show("myVariable can't be empty") Catch ex As Exception MessageBox.Show(ex.Message) Finally MessageBox.Show("We're done") End Try![]()
Last edited by Vswe; 03-21-2010 at 03:33 PM.
What if I wanted to throw my own exception, how would I do that in VB?
Nicely done, +rep!
This was the very last part I wrote before uploading so I completely forgot about it.
Code:Throw New Exception("Oh no, you did something wrong")
Last edited by Vswe; 03-21-2010 at 03:33 PM.
Good thing I'm here to pester you.![]()
That's just good.
SQL statements are a great way to generate exceptions: invalid table/field names, primary key violations, etc. +rep
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks