{"id":4809,"date":"2014-01-26T13:13:24","date_gmt":"2014-01-26T05:13:24","guid":{"rendered":"http:\/\/www.vbtutor.net\/?page_id=4809"},"modified":"2018-06-24T16:54:39","modified_gmt":"2018-06-24T08:54:39","slug":"visual-basic-2013-lesson-22-errors-handling","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/","title":{"rendered":"Visual Basic 2013 Lesson 22: Errors Handling"},"content":{"rendered":"<h4 style=\"text-align: center;\"><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-21-using-radio-button\/\">[Lesson 21]<\/a> &lt;&lt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-tutorial\/\">[Contents]<\/a> &gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-23-creating-web-browser\/\">[Lesson 23]<\/a><\/h4>\n<h3>22.1 Introduction to Errors Handling in Visual Basic 2013<\/h3>\n<p>Error-free code not only enables the program to run smoothly and efficiently, it can also prevent all sorts of problems from happening like program crashes or system hangs.Errors often occur due to incorrect input from the user. For example, the user might make the mistake of attempting to enter text into a textbox that is designed to handle only numeric values, the computer will not be able to perform an arithmetic calculation for text, therefore, will create an error. These errors are known as synchronous errors.<\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"3914691604\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><br \/>\nTherefore a good programmer should be more alert to the parts of the program that could trigger errors and should write errors handling code to help the user in managing the errors. Writing errors handling code is a good practice for Visual Basic 2013 programmers, so do not try to finish a program fast by omitting the errors handling code. However, there should not be too many errors handling code in the program as it creates problems for the programmer to maintain and troubleshoot the program later.Visual Basic 2013 has improved a lot in its built-in errors handling capabilities compared to Visual Basic 6. For example, when the user attempts to divide a number by zero, Visual Basic 2013 will not return an error message but gives the \u2018infinity\u2019 as the answer (although this is mathematically incorrect because it should be undefined)<\/p>\n<h3>22.2 Using On Error GoTo Syntax<\/h3>\n<p>Visual Basic 2013 still supports the VB6 errors handling syntax, that is the On Error GoTo program_label structure. Although it has a more advanced error handling method, we shall deal with that later.We shall now learn how to write errors handling code in Visual Basic 2013. The syntax for errors handling is<\/p>\n<p><strong>On Error GoTo program_label<\/strong><\/p>\n<p>where program_label is the section of code that is designed by the programmer to handle the error committed by the user. Once an error is detected, the program will jump to the program_label section for error handling.<\/p>\n<h4><strong>Example 22.1: The\u00a0Division Error<\/strong><\/h4>\n<p>In this example, we will deal with the error of entering non-numeric data into the text boxes that suppose to hold numeric values. The program_label here is error_handler. when the user enters a non-numeric value into the text boxes, the error message will display the text\u201dOne or both of the entries is\/are non-numeric!\u201d. If no error occurs, it will display the correct answer. Try it out yourself.<\/p>\n<h4><strong>The Code<\/strong><\/h4>\n<p>Private Sub BtnCal_Click(sender As Object, e As EventArgs) Handles BtnCal.Click<br \/>\nLbl_ErrMsg.Visible = False<\/p>\n<p>Dim firstNum, secondNum As Double<\/p>\n<p>On Error GoTo error_handler<\/p>\n<p>firstNum = TxtNum1.Text<br \/>\nsecondNum = TxtNum2.Text<br \/>\nLbl_Answer.Text = firstNum \/ secondNum<\/p>\n<p>Exit Sub\u00a0<span style=\"color: #008000;\"> &#8216;To prevent error handling even the inputs are valid<\/span><\/p>\n<p>error_handler:<\/p>\n<p>Lbl_Answer.Text = &#8220;Error&#8221;<br \/>\nLbl_ErrMsg.Visible = True<br \/>\nLbl_ErrMsg.Text = &#8221; One or both of the entries is\/are non-numeric! Try again!&#8221;<\/p>\n<p>End Sub<\/p>\n<p>The runtime interface<\/p>\n<p><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure22.1.jpg\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-4822\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure22.1.jpg\" alt=\"vb2013_figure22.1\" width=\"60%\" height=\"auto\" \/><\/a><\/p>\n<p style=\"text-align: center;\">\u00a0<strong>Figure 22.1<\/strong><\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"3914691604\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p style=\"text-align: left;\">*Please note that division by zero in Visual Basic 2013 no longer gives an error message, but it displays the answer as Infinity.<\/p>\n<h3>22.3 Errors Handling using Try\u2026..Catch\u2026.End Try Structure<\/h3>\n<p>Visual Basic 2013 has adopted a new approach in handling errors or rather exceptions handling. It is supposed to be more efficient than the old On Error Goto method, where it can handle various types of errors within the Try\u2026Catch\u2026End Try structure.<\/p>\n<p>The structure looks like this<\/p>\n<p><strong>Try<\/strong><\/p>\n<p>statements<\/p>\n<p><strong>Catch<\/strong> exception_variable as Exception<\/p>\n<p>statements to deal with exceptions<\/p>\n<p><strong>End Try<\/strong><\/p>\n<p><strong>The Code<\/strong><\/p>\n<p>Private Sub BtnCal_Click(sender As Object, e As EventArgs) Handles BtnCal.Click<br \/>\nLbl_ErrMsg.Visible = False<\/p>\n<p>Dim firstNum, secondNum, answer As Double<\/p>\n<p>Try<\/p>\n<p>firstNum = TxtNum1.Text<br \/>\nsecondNum = TxtNum2.Text<br \/>\nanswer = firstNum \/ secondNum<br \/>\nLbl_Answer.Text = answer<\/p>\n<p>Catch ex As Exception<\/p>\n<p>Lbl_Answer.Text = &#8220;Error&#8221;<br \/>\nLbl_ErrMsg.Visible = True<br \/>\nLbl_ErrMsg.Text = &#8221; One of the entries is not a number! Try again!&#8221;<\/p>\n<p>End Try<\/p>\n<p>End Sub<\/p>\n<p>The runtime interface<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure22.2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4827\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure22.2.jpg\" alt=\"vb2013_figure22.2\" width=\"358\" height=\"309\" \/><\/a><strong>Figure 22.2<\/strong><\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block;\" data-ad-format=\"autorelaxed\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"3644929102\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4 style=\"text-align: center;\"><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-21-using-radio-button\/\">[Lesson 21]<\/a>\u00a0&lt;&lt;\u00a0<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-tutorial\/\">[Contents]<\/a>\u00a0&gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-23-creating-web-browser\/\">[Lesson 23]<\/a><\/h4>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 21] &lt;&lt; [Contents] &gt;&gt; [Lesson 23] 22.1 Introduction to Errors Handling in Visual Basic 2013 Error-free code not only enables the program to run smoothly and efficiently, it can also prevent all sorts of problems from happening like program crashes or system hangs.Errors often occur due to incorrect input from the user. For example, &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2013 Lesson 22: Errors Handling<\/span><\/a><\/p>\n","protected":false},"author":23013,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-4809","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Visual Basic 2013 Lesson 22: Errors Handling - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"This article discusses errors handling in visual basic 2013\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visual Basic 2013 Lesson 22: Errors Handling - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"This article discusses errors handling in visual basic 2013\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Vbtutor\" \/>\n<meta property=\"article:modified_time\" content=\"2018-06-24T08:54:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure22.1.jpg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@liewvk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/\",\"url\":\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/\",\"name\":\"Visual Basic 2013 Lesson 22: Errors Handling - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"isPartOf\":{\"@id\":\"https:\/\/www.vbtutor.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure22.1.jpg\",\"datePublished\":\"2014-01-26T05:13:24+00:00\",\"dateModified\":\"2018-06-24T08:54:39+00:00\",\"description\":\"This article discusses errors handling in visual basic 2013\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/#primaryimage\",\"url\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure22.1.jpg\",\"contentUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure22.1.jpg\",\"width\":358,\"height\":309},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2013 Lesson 22: Errors Handling\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vbtutor.net\/#website\",\"url\":\"https:\/\/www.vbtutor.net\/\",\"name\":\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"description\":\"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Visual Basic 2013 Lesson 22: Errors Handling - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This article discusses errors handling in visual basic 2013","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:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2013 Lesson 22: Errors Handling - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This article discusses errors handling in visual basic 2013","og_url":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/","og_site_name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","article_publisher":"https:\/\/www.facebook.com\/Vbtutor","article_modified_time":"2018-06-24T08:54:39+00:00","og_image":[{"url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure22.1.jpg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_site":"@liewvk","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/","url":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/","name":"Visual Basic 2013 Lesson 22: Errors Handling - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","isPartOf":{"@id":"https:\/\/www.vbtutor.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/#primaryimage"},"image":{"@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure22.1.jpg","datePublished":"2014-01-26T05:13:24+00:00","dateModified":"2018-06-24T08:54:39+00:00","description":"This article discusses errors handling in visual basic 2013","breadcrumb":{"@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/#primaryimage","url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure22.1.jpg","contentUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure22.1.jpg","width":358,"height":309},{"@type":"BreadcrumbList","@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-22-errors-handling\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2013 Lesson 22: Errors Handling"}]},{"@type":"WebSite","@id":"https:\/\/www.vbtutor.net\/#website","url":"https:\/\/www.vbtutor.net\/","name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/4809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/users\/23013"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=4809"}],"version-history":[{"count":37,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/4809\/revisions"}],"predecessor-version":[{"id":13076,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/4809\/revisions\/13076"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=4809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=4809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=4809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}