{"id":7202,"date":"2015-09-05T21:47:23","date_gmt":"2015-09-05T13:47:23","guid":{"rendered":"http:\/\/www.vbtutor.net\/?page_id=7202"},"modified":"2018-06-22T20:51:26","modified_gmt":"2018-06-22T12:51:26","slug":"visual-basic-2015-lesson-24-handling-errors","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-24-handling-errors\/","title":{"rendered":"Visual Basic 2015 Lesson 24: Handling Errors"},"content":{"rendered":"<h4 style=\"text-align: center;\"><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-23creating-web-browser\/\">[Lesson 23]<\/a> &lt;&lt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-tutorial\/\">[Contents]<\/a> &gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-25-object-oriented-programming\/\">[Lesson 25]<\/a><\/h4>\n<h3>24.1 Introduction to Errors Handling in Visual Basic 2015<\/h3>\n<p>Errors handling is an essential procedure in Visual Basic 2015\u00a0programming. 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 (string) to a box that is designed to handle only numeric values such as the weight of a person, 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=\"4768455349\"><\/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 2015 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 might create problems for the programmer to maintain and troubleshoot the program later.<\/p>\n<p>Visual Basic 2015 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 2015 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<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=\"4768455349\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3>24.2 Using On Error GoTo Syntax<\/h3>\n<p>Visual Basic 2015 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 2015. The syntax for errors handling is<\/p>\n<pre style=\"font-size: 110%;\">On Error GoTo program_label<\/pre>\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 24.1: Division Errors<\/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<pre style=\"font-size: 110%;\">Private Sub BtnCal_Click(sender As Object, e As EventArgs) Handles BtnCal.Click\r\nLbl_ErrMsg.Visible = False\r\n\r\nDim firstNum, secondNum As Double\r\n\r\nOn Error GoTo error_handler\r\n\r\nfirstNum = TxtNum1.Text\r\nsecondNum = TxtNum2.Text\r\nLbl_Answer.Text = firstNum \/ secondNum\r\n\r\nExit Sub\u00a0<span style=\"color: #008000;\"> 'To prevent error handling even the inputs are valid<\/span>\r\n\r\nerror_handler:\r\n\r\nLbl_Answer.Text = \"Error\"\r\nLbl_ErrMsg.Visible = True\r\nLbl_ErrMsg.Text = \" One or both of the entries is\/are non-numeric! Try again!\"\r\n\r\nEnd Sub\r\n<\/pre>\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 loading=\"lazy\" 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=\"358\" height=\"309\" \/><\/a><\/p>\n<p style=\"text-align: center;\">\u00a0<strong>Figure 24.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=\"4768455349\"><\/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 2015\u00a0no longer gives an error message, but it displays the answer as Infinity.<\/p>\n<h3>24.3 Errors Handling using Try\u2026..Catch\u2026.End Try Structure<\/h3>\n<p>Visual Basic 2015 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<pre style=\"font-size: 110%;\">Try\r\n\r\nstatements\r\n\r\nCatch exception_variable as Exception\r\n\r\nstatements to deal with exceptions\r\n\r\nEnd Try<\/pre>\n<h4><strong>The Code<\/strong><\/h4>\n<pre style=\"font-size: 110%;\">Private Sub BtnCal_Click(sender As Object, e As EventArgs) Handles BtnCal.Click\r\nLbl_ErrMsg.Visible = False\r\n\r\nDim firstNum, secondNum, answer As Double\r\n\r\nTry\r\n\r\nfirstNum = TxtNum1.Text\r\nsecondNum = TxtNum2.Text\r\nanswer = firstNum \/ secondNum\r\nLbl_Answer.Text = answer\r\n\r\nCatch ex As Exception\r\n\r\nLbl_Answer.Text = \"Error\"\r\nLbl_ErrMsg.Visible = True\r\nLbl_ErrMsg.Text = \" One of the entries is not a number! Try again!\"\r\n\r\nEnd Try\r\n\r\nEnd Sub\r\n<\/pre>\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 24.2<\/strong><\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-3033628290023372\"\n     data-ad-slot=\"5090773108\"><\/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-2015-lesson-23creating-web-browser\/\">[Lesson 23]<\/a>\u00a0&lt;&lt;\u00a0<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-tutorial\/\">[Contents]<\/a>\u00a0&gt;&gt;<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-25-object-oriented-programming\/\"> [Lesson 25]<\/a><\/h4>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 23] &lt;&lt; [Contents] &gt;&gt; [Lesson 25] 24.1 Introduction to Errors Handling in Visual Basic 2015 Errors handling is an essential procedure in Visual Basic 2015\u00a0programming. 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 &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-24-handling-errors\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2015 Lesson 24: Handling Errors<\/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-7202","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 2015 Lesson 24: Handling Errors - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"This article describe error handling in visual basic 2015\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson24.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visual Basic 2015 Lesson 24: Handling Errors - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"This article describe error handling in visual basic 2015\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson24.html\" \/>\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-22T12:51:26+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-2015-lesson-24-handling-errors\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson24.html\",\"name\":\"Visual Basic 2015 Lesson 24: Handling Errors - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"isPartOf\":{\"@id\":\"https:\/\/www.vbtutor.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson24.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson24.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure22.1.jpg\",\"datePublished\":\"2015-09-05T13:47:23+00:00\",\"dateModified\":\"2018-06-22T12:51:26+00:00\",\"description\":\"This article describe error handling in visual basic 2015\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson24.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson24.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson24.html#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\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson24.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2015 Lesson 24: Handling Errors\"}]},{\"@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 2015 Lesson 24: Handling Errors - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This article describe error handling in visual basic 2015","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":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson24.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2015 Lesson 24: Handling Errors - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This article describe error handling in visual basic 2015","og_url":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson24.html","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-22T12:51:26+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-2015-lesson-24-handling-errors\/","url":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson24.html","name":"Visual Basic 2015 Lesson 24: Handling Errors - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","isPartOf":{"@id":"https:\/\/www.vbtutor.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson24.html#primaryimage"},"image":{"@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson24.html#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure22.1.jpg","datePublished":"2015-09-05T13:47:23+00:00","dateModified":"2018-06-22T12:51:26+00:00","description":"This article describe error handling in visual basic 2015","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson24.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson24.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson24.html#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":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson24.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2015 Lesson 24: Handling Errors"}]},{"@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\/7202","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=7202"}],"version-history":[{"count":32,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/7202\/revisions"}],"predecessor-version":[{"id":13040,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/7202\/revisions\/13040"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=7202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=7202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=7202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}