{"id":126,"date":"2011-11-24T14:25:27","date_gmt":"2011-11-24T06:25:27","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=126"},"modified":"2018-09-03T21:19:05","modified_gmt":"2018-09-03T13:19:05","slug":"some-information-about-exceptions-in-software-systems","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/some-information-about-exceptions-in-software-systems\/","title":{"rendered":"Some information about exceptions in software systems"},"content":{"rendered":"<p>Throughout my software development exposures, defining and handling exceptions has always constituted a fair amount of my programming considerations. This post collates information about exceptions that I had came across.      <\/p>\n<h3>The need for handling errors in your codes<\/h3>\n<p>Handling errors in your codes is intuitive - you are writing codes that people will use and people make mistakes. A good read of <a href=\"http:\/\/astore.amazon.com\/clivsperswebs-20\/detail\/0321767535\" target=\"_blank\">100 things every designer needs to know about people<\/a> by Susan Weinschenk will give you great insights on that. <\/p>\n<h4>Preventing people from making mistakes<\/h4>\n<p>Perhaps it may be better to prevent people from making mistakes while using a program feature, but there are times when it is beyond our control or technically challenging. A batch processing job that receives work input from the HTML file upload dialog box can be given a file type that is not expected. Even if we implement restriction on the file type, there is still no way to stop mischievous users from uploading files with invalid data format.<\/p>\n<h4>Unavoidable mistakes<\/h4>\n<p>The people part is one part of the story; components that you use in your code may fail too. Insufficient storage, invalid permissions, server does not like your request and what not. For example, making a <a href=\"http:\/\/www.w3.org\/TR\/wsdl\" target=\"_blank\" title=\"Web Services Description Language\">WSDL<\/a> call via C# codes generated from <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/7h3ystb6%28v=vs.80%29.aspx\" title=\"Web Services Description Language Tool\">wsdl.exe<\/a> may result in <\/p>\n<ol>\n<li>Server code encountering errors in processing the wsdl call.<\/li>\n<li>Server process could not find the code to handle the wsdl call.<\/li>\n<li>Server refusing the client request.<\/li>\n<li>Client cannot connect to the server at the network layer.<\/li>\n<li>Client does not have network connection.<\/li>\n<li>And many unexpected ones like Microsoft fly-by compiler attempts to create a temporary file but the operating system doesn't have a temporary folder.<\/li>\n<\/ol>\n<h3>What are exceptions?<\/h3>\n<p>Exceptions are programming constructs that encapsulate error information. In an object oriented environment, they are usually defined with a generic exception construct and thrown around when unwanted conditions occurs. <\/p>\n<p>Suppose that we want to define an exception that is thrown when our program cannot initialise itself due to missing prerequisites. One way to do so in C# is as follows:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class MissingPrerequisitesException\r\n: ApplicationException\r\n{\r\n    \/\/ Declare an enum that provides a set of\r\n    \/\/ reasons for callers\r\n    public enum ExceptionReason\r\n    {\r\n        MissingComponents,\r\n        MissingConfigurationFile,\r\n        MissingConfigurations,\r\n    };\r\n\r\n    \/\/ Take note of the reason\r\n    private ExceptionReason _reason;\r\n\r\n    public ExceptionReason Reason\r\n    {\r\n        get\r\n        {\r\n            return this._reason;\r\n        }\r\n\r\n        private set\r\n        {\r\n            this._reason = value;\r\n        }\r\n    } \r\n \r\n    public MissingPrerequisitesException(\r\n        ExceptionReason reason,\r\n        string message)\r\n        : base(message)\r\n    {\r\n        this.Reason = reason;\r\n    }\r\n\r\n    public MissingPrerequisitesException(\r\n        ExceptionReason reason,\r\n        string message,\r\n        Exception inner)\r\n        : base(message, inner)\r\n    {\r\n        this.Reason = reason;\r\n    }\r\n\r\n} \/\/ end public class MissingPrerequisitesException\r\n<\/pre>\n<p>And when our program detects a missing configuration file, it can throw an instance of the exception class with the <code>throw<\/code> keyword:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic void LoadConfigurations()\r\n{\r\n    if (!File.Exists(&quot;config-file.txt&quot;))\r\n    {\r\n        throw new MissingPrerequisitesException(\r\n            MissingPrerequisitesException.ExceptionReason.MissingConfigurations,\r\n            &quot;Configuration file does not exists!&quot;);\r\n    } \/\/ end if\r\n\r\n    \/\/ Read configurations from the file\r\n    \/\/ ...\r\n\r\n} \/\/ end public void LoadConfigurations()\r\n<\/pre>\n<h3>Why exceptions?<\/h3>\n<h4>Exceptions contain rich information about an error that happened<\/h4>\n<p>Implicitly, exceptions contain information for developers to know how and what error had happened. <\/p>\n<p>For instance, the caller of <code>LoadConfigurations<\/code> can enclose the call in a try-catch block and print the exception stack trace:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprivate void Init()\r\n{\r\n    try\r\n    {\r\n        LoadConfigurations();\r\n        \/\/ Configure other components\r\n        \/\/ ...\r\n    }\r\n    catch (MissingPrerequisitesException mpe)\r\n    {\r\n        \/\/ The error that occurred is due to\r\n        \/\/ missing prerequisites\r\n        Console.WriteLine(&quot;Missing prerequisites!&quot;);\r\n        \/\/ Print the message\r\n        Console.WriteLine(mpe.);\r\n        \/\/ Print the stack trace\r\n        Console.WriteLine(mpe.StackTrace);\r\n\r\n    }\r\n} \/\/ end private void init()\r\n<\/pre>\n<p>And when the config-file.txt file cannot be found by the program, the program will output something like this:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nMissing prerequisites!\r\nConfiguration file does not exists!\r\n   at Program.LoadConfigurations() in d:\\poc\\exceptions\\Program.cs:line 47\r\n   at Program.Init() in d:\\poc\\exceptions\\Program.cs:line 27\r\n<\/pre>\n<p>The 3rd line is the stack trace. Reading from the bottom up (from the 4th line), a developer can know that there is a Program.cs file and the error happens in the following sequence: <\/p>\n<ol>\n<li>\nThe <code>Init<\/code> method is called<\/li>\n<li>In the Init() method, the <code>LoadConfigurations<\/code> method is called.<\/li>\n<\/ol>\n<p>And because the <code>LoadConfigurations<\/code> method is at the top of the stack trace, it is the method that reports a missing configuration file.  <\/p>\n<p>Those are standard items that our MissingPrequisitesException class inherits from its parent class. We can also introduce more information within our custom defined exceptions to aid the exception handling code in reacting to the exception. For instance, with the <code>Reason<\/code> property that was introduced in the MissingPrequisitesException class, the exception handling code can react to different cases of missing prerequisites with a <code>switch<\/code> statement:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nswitch(mpe.Reason) \r\n{\r\n    case MissingPrerequisitesException.ExceptionReason.MissingComponents:\r\n        Console.WriteLine(&quot;Missing components, please check your installation.&quot;);\r\n        break;\r\n    case MissingPrerequisitesException.ExceptionReason.MissingConfigurationFile:\r\n        Console.WriteLine(&quot;Missing configuration file, make sure that config-file.txt is in the same directory as the running program.&quot;);\r\n        break;\r\n    case MissingPrerequisitesException.ExceptionReason.MissingConfigurations:\r\n        Console.WriteLine(&quot;Missing configurations, make sure that all needed configurations are supplied via config-file.txt.&quot;);\r\n        break;\r\n} \/\/ end switch(mpe.Reason)\r\n<\/pre>\n<h3>Some articles which helped me gained great insights to the area of exceptions<\/h3>\n<ul>\n<li><a href=\"http:\/\/www.codeproject.com\/KB\/cpp\/cppexceptionsproetcontra.aspx\" title=\"C++ Exceptions: Pros and Cons\" target=\"_blank\">C++ Exceptions: Pros and Cons<\/a><\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms229014.aspx\" title=\"Design Guidelines for Exceptions\" target=\"_blank\">Design Guidelines for Exceptions<\/a><\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/seyhszts.aspx\" title=\"Best Practices for Handling Exceptions\" target=\"_blank\">Best Practices for Handling Exceptions in .NET<\/a><\/li>\n<li><a href=\"http:\/\/wikijava.org\/wiki\/10_best_practices_with_Exceptions\" title=\"10 Best Practices With Exceptions (Java)\" target=\"_blank\">10 Best Practices With Exceptions<\/a><\/li>\n<\/ul>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-22\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-22&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-22&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F126&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>Throughout my software development exposures, defining and handling exceptions has always constituted a fair amount of my programming considerations. This post collates information about exceptions that I had came across.  <\/p>\n","protected":false},"author":1,"featured_media":1203,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[20,68],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Warning-Sign.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-22","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/126"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=126"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/126\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1203"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}