{"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

\"basic_error_handling_in_vb_01\"<\/p>\n

Click on Create next to Project to create our project.<\/p>\n

\"basic_error_handling_in_vb_02\"<\/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

\"basic_error_handling_in_vb_03\"
\n\"basic_error_handling_in_vb_04\"<\/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>\n

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 \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

\"basic_error_handling_in_vb_05\"<\/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

\"basic_error_handling_in_vb_06\"<\/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

\"basic_error_handling_in_vb_07\"<\/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

\"basic_error_handling_in_vb_08\"<\/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

\"basic_error_handling_in_vb_09\"<\/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":"\nHandling Errors To Prevent Application Crashes in VB<\/title>\n<meta name=\"description\" content=\"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.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/teamtutorials.com\/programming-tutorials\/handling-error-to-prevent-application-crashes-in-vb\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mike Maguire\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/teamtutorials.com\/#website\",\"url\":\"https:\/\/teamtutorials.com\/\",\"name\":\"Team Tutorials\",\"description\":\"Learn something new today\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/teamtutorials.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/handling-error-to-prevent-application-crashes-in-vb#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/teamtutorials.com\/wp-content\/uploads\/2009\/02\/basic_error_handling_in_vb_01.jpg\",\"contentUrl\":\"http:\/\/teamtutorials.com\/wp-content\/uploads\/2009\/02\/basic_error_handling_in_vb_01.jpg\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/handling-error-to-prevent-application-crashes-in-vb#webpage\",\"url\":\"https:\/\/teamtutorials.com\/programming-tutorials\/handling-error-to-prevent-application-crashes-in-vb\",\"name\":\"Handling Errors To Prevent Application Crashes in VB\",\"isPartOf\":{\"@id\":\"https:\/\/teamtutorials.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/handling-error-to-prevent-application-crashes-in-vb#primaryimage\"},\"datePublished\":\"2009-02-27T10:35:49+00:00\",\"dateModified\":\"2009-02-27T10:37:39+00:00\",\"author\":{\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b\"},\"description\":\"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.\",\"breadcrumb\":{\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/handling-error-to-prevent-application-crashes-in-vb#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/teamtutorials.com\/programming-tutorials\/handling-error-to-prevent-application-crashes-in-vb\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/handling-error-to-prevent-application-crashes-in-vb#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/teamtutorials.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Handling Errors To Prevent Application Crashes in VB\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b\",\"name\":\"Mike Maguire\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r\",\"caption\":\"Mike Maguire\"},\"sameAs\":[\"http:\/\/mikemaguire.me\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Handling Errors To Prevent Application Crashes in VB","description":"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.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/teamtutorials.com\/programming-tutorials\/handling-error-to-prevent-application-crashes-in-vb","twitter_misc":{"Written by":"Mike Maguire","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/teamtutorials.com\/#website","url":"https:\/\/teamtutorials.com\/","name":"Team Tutorials","description":"Learn something new today","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/teamtutorials.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/programming-tutorials\/handling-error-to-prevent-application-crashes-in-vb#primaryimage","inLanguage":"en-US","url":"http:\/\/teamtutorials.com\/wp-content\/uploads\/2009\/02\/basic_error_handling_in_vb_01.jpg","contentUrl":"http:\/\/teamtutorials.com\/wp-content\/uploads\/2009\/02\/basic_error_handling_in_vb_01.jpg"},{"@type":"WebPage","@id":"https:\/\/teamtutorials.com\/programming-tutorials\/handling-error-to-prevent-application-crashes-in-vb#webpage","url":"https:\/\/teamtutorials.com\/programming-tutorials\/handling-error-to-prevent-application-crashes-in-vb","name":"Handling Errors To Prevent Application Crashes in VB","isPartOf":{"@id":"https:\/\/teamtutorials.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/teamtutorials.com\/programming-tutorials\/handling-error-to-prevent-application-crashes-in-vb#primaryimage"},"datePublished":"2009-02-27T10:35:49+00:00","dateModified":"2009-02-27T10:37:39+00:00","author":{"@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b"},"description":"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.","breadcrumb":{"@id":"https:\/\/teamtutorials.com\/programming-tutorials\/handling-error-to-prevent-application-crashes-in-vb#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/teamtutorials.com\/programming-tutorials\/handling-error-to-prevent-application-crashes-in-vb"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/teamtutorials.com\/programming-tutorials\/handling-error-to-prevent-application-crashes-in-vb#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/teamtutorials.com\/"},{"@type":"ListItem","position":2,"name":"Handling Errors To Prevent Application Crashes in VB"}]},{"@type":"Person","@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b","name":"Mike Maguire","image":{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r","caption":"Mike Maguire"},"sameAs":["http:\/\/mikemaguire.me"]}]}},"_links":{"self":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/1776"}],"collection":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/comments?post=1776"}],"version-history":[{"count":0,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/1776\/revisions"}],"wp:attachment":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/media?parent=1776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/categories?post=1776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/tags?post=1776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}