{"id":1776,"date":"2009-02-27T06:35:49","date_gmt":"2009-02-27T10:35:49","guid":{"rendered":"http:\/\/teamtutorials.com\/?p=1776"},"modified":"2009-02-27T06:37:39","modified_gmt":"2009-02-27T10:37:39","slug":"handling-error-to-prevent-application-crashes-in-vb","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/programming-tutorials\/handling-error-to-prevent-application-crashes-in-vb","title":{"rendered":"Handling Errors To Prevent Application Crashes in VB"},"content":{"rendered":"
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\u2019s create a new project in Visual Basic.<\/p>\n
<\/p>\n
Click on Create next to Project to create our project.<\/p>\n
<\/p>\n
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. <\/p>\n
\n<\/p>\n
Note that the plus sign is just a label with the \u201c+\u201d plus symbol as the text. Then we have two textboxes (txt1 and txt2) and finally a command button (cmdCalculate). Now, let\u2019s double click on the button to begin our programming. <\/p>\n
\r\n Try\r\n MsgBox(CInt(txt1.Text) + CInt(txt2.Text))\r\n Catch ex As Exception\r\n MsgBox("This message shows when it errors to tell you why:" & ex.Message)\r\n Finally\r\n MsgBox("This message shows whether it errors or not")\r\n txt1.Text = Nothing\r\n txt2.Text = Nothing\r\n End Try\r\n<\/pre>\nAll 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 \u201cTry\u201d 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 \u201cCatch\u201d section will be executed in the event that anything in the \u201cTry\u201d statement fails. Here we are popping a message box up and giving the user the information of why it failed. The \u201cex.message\u201d 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\u2019s run it and make sure it does what it is supposed to.<\/p>\n
<\/p>\n
Let\u2019s start by putting in two numbers to make sure it adds them together correctly. I put in 34 and 54. <\/p>\n
<\/p>\n
You should get the above message box if it worked. Mine came to 88 which means it is working as it is expected.<\/p>\n
<\/p>\n
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. <\/p>\n
<\/p>\n
Lets put 34 in the first box and \u201c35t\u201d (thirty five and then the letter t). Then click the calculate button. <\/p>\n
<\/p>\n
Since we are trying to convert the text in txt2 to an integer and it contains a letter, that won\u2019t work and it will error out with the above message. \u201cConversion from string \u201c35t\u201d to type \u2018Integer\u2019 is not valid\u201d is the text that was stored in \u201cex.message\u201d. Normally the app would have crashed due to an unhandled exception and made the user start over from scratch (which in our case isn\u2019t 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). <\/p>\n
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.<\/p>\n","protected":false},"excerpt":{"rendered":"
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.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[25,26],"tags":[],"yoast_head":"\n
Handling Errors To Prevent Application Crashes in VB<\/title>\n\n\n\n\n\t\n\t\n\t\n