This tutorial will walk you through a very simple but extremely useful function in programming. When you write code you can never guarantee that specific requirements will be met upon execution and therefore errors are always a possibility. As a coder you need to be able foresee these types of issues and catch them before the application crashes and requires the user to restart it and even worse, lose data that they may have been working on. To do this, we will use a Try Catch Finally statement. We will go into detail in the tutorial. To start, let’s create a new project in Visual Basic.
Click on Create next to Project to create our project.
Name the project what you want and place it where you want as well. I called mine tutorial and stored it in a folder named mike on my desktop. Next we need to make the form. The first picture is what is should look like and the second is the items labeled with what I named each control so that yours can be the same.
Note that the plus sign is just a label with the “+” plus symbol as the text. Then we have two textboxes (txt1 and txt2) and finally a command button (cmdCalculate). Now, let’s double click on the button to begin our programming.
Try MsgBox(CInt(txt1.Text) + CInt(txt2.Text)) Catch ex As Exception MsgBox("This message shows when it errors to tell you why:" & ex.Message) Finally MsgBox("This message shows whether it errors or not") txt1.Text = Nothing txt2.Text = Nothing End Try
All of this code should go in the sub for the button press. This is all the programming we will have to do to understand what this phrase does and why it can be so useful. First we initialize the “Try” statement. Anything under this section will be run once the code gets to this point. You can put multiple things in here and it will step through them until it completes or errors out. Here, we are attempting to convert anything in the textboxes to an integer so that they can be added together and show the result. Anything in the “Catch” section will be executed in the event that anything in the “Try” statement fails. Here we are popping a message box up and giving the user the information of why it failed. The “ex.message” is whatever message the exception that occurred carries so that we can inform the user of why it crashed. Anything in the finally statement will be executed either way. This is useful for connections and stuff so that whether the transaction fails or completes it will close the connection. In this sample, we are making a message box appear as well as clearing both text boxes whether the process fails or not. Finally we end the try statement. Let’s run it and make sure it does what it is supposed to.
Let’s start by putting in two numbers to make sure it adds them together correctly. I put in 34 and 54.
You should get the above message box if it worked. Mine came to 88 which means it is working as it is expected.
Then you will get the above message box as well because it is in the finally section of the code which is run either way. The textboxes should now clear as well. Next we will try to make sure our try, catch statement works properly.
Lets put 34 in the first box and “35t” (thirty five and then the letter t). Then click the calculate button.
Since we are trying to convert the text in txt2 to an integer and it contains a letter, that won’t work and it will error out with the above message. “Conversion from string “35t” to type ‘Integer’ is not valid” is the text that was stored in “ex.message”. Normally the app would have crashed due to an unhandled exception and made the user start over from scratch (which in our case isn’t a big deal, but when you are working on more advanced apps and a lot of data is involved it can be very annoying to have to start over).
Finally, you will get the message that appears whether the try works or not and the text boxes will be cleared. That concludes this tutorial. I hope you can see why the try, catch, finally statement is so important. It is a very common part of programming and is used to some extent in every language. Once you get used to it, you can use multiple catch statements to catch different types of errors and do different things based on what type of error is caught. I hope this is helpful and that you were able to understand. Thanks for reading.