Basic Information on Exception Handling in .Net #.Net Web Applications

Brought from our old site: http://salearningschool.com/displayArticle.php?table=Articles&articleID=1356&title=Basics%20of%20Exception%20Handling%20in%20.Net

Basic Information on Exception Handling in .Net

Why exception handling? In your applications, unexpected error situations may occur that are out of your application scope and your application logic. These are actually external to your application. For example, while you are reading a file from hard drive, the file may not be there; or the remote server may be too busy or unavailable to get back to you with the result of your query. Exception handling addresses such situations.

How can you handle such situations:

  • Use try, catch, finally structure to address such situations
  • Put the code where, you are anticipating an exception inside a try{} block. Put the code to handle the error situation in catch (Exception ex) {} blocks. You can have multiple catch blocks where each one will address a specific type of exception situation.
  • One more note, the code block where exception may occur may need to execute some logic/code whether an exception occurs or or not such as closing a file, closing the database connection. To accomplish this, you can use a finally block. A finally block is always executed, regardless of whether an exception is thrown or not.
  • When an exception occurs, the events are thrown by the environment. You need to address that if you want to. Else, your users will see the errors and will cause a bad user experience.
  • You can even throw exceptions on your own in your code. But why? say if you anticipate that there will be some error situations that are not external but related to your application logic and you actually do not want to handle those situations (serve some services in those situations for example) in your application. You can just throw exceptions and think that these are the conditions; you do not want to support in your application. Throw exception and provide some generalized response to your visitors.
  • You can also handle exceptions throw the web objects' built in error events such as Page_Error, Global_Error, Application_Error.
  • In certain situations, You can take advantage of the error pages of the webserver to provide useful information to your visitors. You can replace the default error pages of your web-server and provide customized error pages to your visitor
  • Finally, you can use tracing to log the error events. Monitor the tracing to understand which types of situations occur more frequently than others. And find out a solution and apply the solution
  • It is possible to turn on and off this tracing without modifying the application code. You can make use of the web.config file configurations

Regarding error pages, you can provide your custom error pages for the standard HTTP error codes. Some codes are given below:204:No Content, 301:Moved, Moved Permanently, 302: Found, Redirect, 400:bad request, 401:unauthorized, 403: forbidden, 404:not found, 408:request time out, 500:internal server error, 503:service unavailable, 505:Http version not supported.

You can also make use of the errorPage attribute of the Page object to define an error page for the exceptions of that particular page. Page level settings override the application level settings (as provided by web.config file)

  • How to enable application level tracing: use something similar as below in your web.config file
    { and } are used instead of < and > respectively.
        {configuration}
            {system.web}
                {trace enabled="true" requestLimit="40" localOnly="false"/}
            {/system.web}
        {/configuration}
    
  • to enable tracing for a particular page, use the trace property of the Document object
  • how to write tracing information to the trace log: Trace.Warn()

From: http://sitestree.com/?p=3744
Categories:.Net Web Applications
Tags:
Post Data:2016-07-16 12:23:19

    Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
    (Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a> 
    In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
    <a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
    8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
    Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
    Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>

Leave a Reply