{"id":1108,"date":"2012-04-06T16:44:58","date_gmt":"2012-04-06T08:44:58","guid":{"rendered":"http:\/\/www.vbtutor.net\/index.php\/"},"modified":"2018-06-24T22:40:54","modified_gmt":"2018-06-24T14:40:54","slug":"visual-basic-2010-lesson-20","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-20\/","title":{"rendered":"Visual Basic 2010 Lesson 20 &#8211; Errors Handling"},"content":{"rendered":"<h4 align=\"center\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-19\/\">[Lesson 19]<\/a> <\/strong>&lt;&lt;\u00a0<strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-tutorial\/\">[CONTENTS]<\/a> &gt;&gt;<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-21\/\"> [Lesson 21]<\/a><\/strong><\/h4>\n<h3>20.1 Introduction<\/h3>\n<p>Error handling is an essential procedure in Visual Basic 2010 programming that helps make a program error-free. An error-free program can run smoothly and efficiently, and the user does not have to face all sorts of problems such as 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 the arithmetic calculation for text, therefore, will create an error. These errors are known as synchronous errors.<\/p>\n<p>Therefore 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 2010 programmers, so do not try to finish a program fast by omitting the errors handling code. On the other hand, 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. VB2010 has improved a lot in its built-in errors handling capabilities compared to older version versions of VB.<br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\"\n     style=\"display:block; text-align:center;\"\n     data-ad-layout=\"in-article\"\n     data-ad-format=\"fluid\"\n     data-ad-client=\"ca-pub-3033628290023372\"\n     data-ad-slot=\"1723562988\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><br \/>\nFor example, when the user attempts to divide a number by zero, Vb2010 will not return an error message but gives the &#8216;infinity&#8217; as the answer (although this is mathematically incorrect because it should be undefined)<\/p>\n<h3>20.2 Using On Error GoTo Syntax<\/h3>\n<p>Visual Basic 2010 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 VB2010. The syntax for errors handling is<\/p>\n<pre style=\"font-size: 110%; width: 80%;\">On Error GoTo   program_label\r\n<\/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<p><strong>Example 20.1: Division by Zero<\/strong><\/p>\n<p>In this example, we will deal with the error of entering non-numeric data into the textboxes that suppose to hold numeric values. The program_label here is error_hanldler. when the user enters non-numeric values into the textboxes, the error message will display the text&#8221;One of the entries is not a number! Try again!&#8221;. If no error occurs, it will display the correct answer. Try it out yourself.<\/p>\n<p><strong>The Code<\/strong><\/p>\n<pre style=\"font-size: 110%;\">Public Class Form1\r\n\r\nPrivate Sub CmdCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdCalculate.Click\r\nLbl_ErrorMsg.Visible = False\r\nDim firstNum, secondNum As Double\r\n On Error GoTo error_handler\r\n firstNum = Txt_FirstNumber.Text\r\n secondNum = Txt_SecondNumber.Text\r\n Lbl_Answer.Text = firstNum \/ secondNum\r\nExit Sub 'To prevent error handling even the inputs are valid\r\n\r\nerror_handler:\r\n\r\n Lbl_Answer.Text = \"Error\"\r\n Lbl_ErrorMsg.Visible = True\r\n Lbl_ErrorMsg.Text = \" One of the entries is not a number! Try again!\"\r\nEnd Sub\r\nEnd Class\r\n<\/pre>\n<p><strong>The Output<\/strong><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/www.vbtutor.net\/vb2008\/Images\/vb2008_20.1.gif\" alt=\"\" width=\"470\" height=\"325\" \/><br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\"\n     style=\"display:block; text-align:center;\"\n     data-ad-layout=\"in-article\"\n     data-ad-format=\"fluid\"\n     data-ad-client=\"ca-pub-3033628290023372\"\n     data-ad-slot=\"1723562988\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3>20.3 Errors Handling using Try&#8230;..Catch&#8230;.End Try Structure<\/h3>\n<p>VB2010 has adopted a new approach in handling errors known as 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&#8230;Catch&#8230;End Try structure.<\/p>\n<p>The structure looks like this<\/p>\n<pre style=\"font-size: 110%; width: 80%;\">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\r\n<\/pre>\n<p><strong>Example 20.2<\/strong><\/p>\n<p>This is a modification of Example 20.1. Instead of using On Error GoTo method, we use the Try&#8230;Catch&#8230;End Try method. In this example, the Catch statement will catch the exception when the user enters a non-numeric data and return the error message. If there is no exception, there will not any action from the Catch statement and the program returns the correct answer.<\/p>\n<h4>The code<\/h4>\n<pre style=\"font-size: 110%;\">Public Class Form1\r\n\r\nPrivate Sub CmdCalculate_Click(ByVal sender As System.Object, ByVal eAs System.EventArgs) Handles CmdCalculate.Click\r\n Lbl_ErrorMsg.Visible = False\r\n\r\nDim firstNum, secondNum, answer As Double\r\n Try\r\n firstNum = Txt_FirstNumber.Text\r\n secondNum = Txt_SecondNumber.Text\r\n answer = firstNum \/ secondNum\r\n Lbl_Answer.Text = answer\r\n Catch ex As Exception\r\n Lbl_Answer.Text = \"Error\"\r\n Lbl_ErrorMsg.Visible = True\r\n Lbl_ErrorMsg.Text = \" One of the entries is not a number! Try again!\"\r\n End Try\r\nEnd Sub\r\nEnd Class\r\n<\/pre>\n<p>The output<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/www.vbtutor.net\/vb2008\/Images\/vb2008_20.2.gif\" alt=\"\" width=\"470\" height=\"325\" \/><br \/>\n<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=\"8075128701\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4 align=\"center\"><strong><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-19\/\">[Lesson 19]<\/a> <\/strong>&lt;&lt;\u00a0<strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-tutorial\/\">[CONTENTS]<\/a> &gt;&gt;<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-21\/\"> [Lesson 21]<\/a><\/strong><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 19] &lt;&lt;\u00a0[CONTENTS] &gt;&gt; [Lesson 21] 20.1 Introduction Error handling is an essential procedure in Visual Basic 2010 programming that helps make a program error-free. An error-free program can run smoothly and efficiently, and the user does not have to face all sorts of problems such as program crashes or system hangs.Errors often occur due &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-20\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2010 Lesson 20 &#8211; 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-1108","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 2010 Lesson 20 - Errors Handling - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"This Visual Basic 2010 lesson shows you how to write errors handling code in Visual Basic 2010\" \/>\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\/vb2010\/vb2010_Lesson20.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visual Basic 2010 Lesson 20 - Errors Handling - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"This Visual Basic 2010 lesson shows you how to write errors handling code in Visual Basic 2010\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson20.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-24T14:40:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vbtutor.net\/vb2008\/Images\/vb2008_20.1.gif\" \/>\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=\"4 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-2010-lesson-20\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson20.html\",\"name\":\"Visual Basic 2010 Lesson 20 - Errors Handling - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"isPartOf\":{\"@id\":\"https:\/\/www.vbtutor.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson20.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson20.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/vb2008\/Images\/vb2008_20.1.gif\",\"datePublished\":\"2012-04-06T08:44:58+00:00\",\"dateModified\":\"2018-06-24T14:40:54+00:00\",\"description\":\"This Visual Basic 2010 lesson shows you how to write errors handling code in Visual Basic 2010\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson20.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson20.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson20.html#primaryimage\",\"url\":\"https:\/\/www.vbtutor.net\/vb2008\/Images\/vb2008_20.1.gif\",\"contentUrl\":\"https:\/\/www.vbtutor.net\/vb2008\/Images\/vb2008_20.1.gif\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson20.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2010 Lesson 20 &#8211; 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 2010 Lesson 20 - Errors Handling - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This Visual Basic 2010 lesson shows you how to write errors handling code in Visual Basic 2010","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\/vb2010\/vb2010_Lesson20.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2010 Lesson 20 - Errors Handling - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This Visual Basic 2010 lesson shows you how to write errors handling code in Visual Basic 2010","og_url":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson20.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-24T14:40:54+00:00","og_image":[{"url":"https:\/\/www.vbtutor.net\/vb2008\/Images\/vb2008_20.1.gif","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_site":"@liewvk","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-20\/","url":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson20.html","name":"Visual Basic 2010 Lesson 20 - Errors Handling - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","isPartOf":{"@id":"https:\/\/www.vbtutor.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson20.html#primaryimage"},"image":{"@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson20.html#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/vb2008\/Images\/vb2008_20.1.gif","datePublished":"2012-04-06T08:44:58+00:00","dateModified":"2018-06-24T14:40:54+00:00","description":"This Visual Basic 2010 lesson shows you how to write errors handling code in Visual Basic 2010","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson20.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson20.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson20.html#primaryimage","url":"https:\/\/www.vbtutor.net\/vb2008\/Images\/vb2008_20.1.gif","contentUrl":"https:\/\/www.vbtutor.net\/vb2008\/Images\/vb2008_20.1.gif"},{"@type":"BreadcrumbList","@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson20.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2010 Lesson 20 &#8211; 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\/1108","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=1108"}],"version-history":[{"count":43,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1108\/revisions"}],"predecessor-version":[{"id":13140,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1108\/revisions\/13140"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=1108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=1108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=1108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}