
How To Handle Errors While Developing ASP.NET ApplicationsDesign of web-based applications needs to address to issues like scalability, security, reliability, performance, extensibility, etc. ASP.net provides a right platform to cater to the above needs. ASP.net is a part of .Net framework that works with IIS (Internet Information Server) to provide dynamic content creation on a web server when they are requested over HTTP. Leveraging on the power of .Net framework, it is used to create web-based applications in a faster and simpler way.
Some of the
main features added to ASP.net from ASP, its earlier version (before existence
of .Net) includes: Better
language support (usage of languages other than VBScript, Jscript, etc.)
and hence better performance Designing
a suitable strategy for handling errors gracefully is a challenge since
it involves maintaining the security and reliability of the system. Error
or exception is a state when the executing code cannot proceed further
due to exceptions thrown by database, ASP.net runtime, third-party software,
etc. By default, ASP.net runtime displays the yellow screen indicating
the error information. Although it is useful in a developer environment,
it cannot be directly used for the production-ready systems since it is
meaningless for a user to know some unnecessary error information. Following
are the three methods for handling errors in ASP.net: Structured
Exception Handling method Structured
Exception Handling method ASP.net offers
structured exception handling mechanism by providing Try..Catch..Finally
blocks wherein the main code for the application logic is within the Try
block while the catch block contains the routine to handle
the exception. On occurrence of an error, an exception object is thrown
by the runtime and the control of execution goes to the Catch
block in which the exception is caught to get the complete information
about the exception and handled accordingly. Exception information include
code location, type, reason for exception, etc. Finally is
the block for executing the code that need to be executed whether there
is exception or not(usually housekeeping code like memory cleanup). Syntax
for using this exception handling method is as below: Try{ //code
underlying the business logic including exceptions thrown by user the
executing code can result in an uncontrollable state (like opening a database,
internet connection, etc.) It is better
to centralize exception handling as this gives a consistent behavior throughout
the application. Error
handlers based on event The two levels
in which event can be handled are Page level and Application level. As
the name implies, errors are handled at the Page and Application level
respectively. Page level
error handler is called when the exception raised is not handled in any
Try..Catch blocks or any other routine in that page. During this error,
there is no access to any controls since they do not exist and hence care
should be taken to not to access them in the error handler. Code for Page
level handlers is within Page_Error method in the .aspx file. It is mostly
used when there is a need to handle error at that stage by logging error
and provide user meaningful information that is relevant for that context.
Application
(or Global) level error handler is called when the exception raised is
not handled in the page wherever the error occurred. Code for Global error
handler is within the global.aspx file. For execution of this handler,
Server.ClearError() should not be called at the page level or any other
method earlier. Also, global handler will not be called for errors that
cannot be detected by ASP.net runtime. Mostly, it is used when there is
a common action (like logging errors, sending mail, etc.) that needs to
be taken for certain type of errors. Custom
Error page ASP.net provides
a simple way to gracefully handle errors by allowing to use custom error
page. By displaying the default error page (as sent directly by the server)
to the end user, it confuses the user more and results in more support
calls. Also, it may sometimes display sensitive information (like password
for opening database) and affect the security of the system. Error pages
are mainly designed for user-friendliness (during production time) or
for debugging purposes while developing the web application. By setting
the following section in web.config file, the Internet information Server
displays the custom error page on error (instead of the default page) Make sure
that the custom error page is accessible by public and available for all
users. Error numbers can be configured and mapped to their corresponding
error pages in the web.config file so that context-sensitive error pages
are displayed in a user-friendly manner. Thus, the
order of propagation of exception handler can be represented as below: Structured
Exception Handler -> Page level handler -> Application level handler
-> Custom Error page. Basic
rules that need to be followed Restrict
the display of detailed error information to only local users of the application Thus, ASP.net
provides multiple options for handling errors in a web-based application
to make it more robust, reliable, secure, consistent and user-friendly.
Tweet
_______________________________________________________________________ _______________________________________________________________________
FREE
Subscription
Subscribe to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ |