The various options available for us for handling errors in asp.net are:
• Code in such a way that possibility of error is less. Try to avoid error conditions as far as possible • Trapping errors as and when they occur. You should be prepared to handle unexpected conditions like network failures, database crashes and the like.
public class TextReader { public string ReadLine() { try { // Read a line from the stream. } catch ( NullReferenceException NRE ) { Response.Write("Null Reference Exception "); } catch ( Exception DefaultExcep ) { Response.Write("Error Occured, Unknown Orgin"); } finally { Response.Write("Caught Exception"); } } }
We can identify business error conditions in code itself rather than relaying on some database stored error codes.
We can provide much detailed and user-friendly information to the users.
public class TextReader { public string ReadLine() { try { // Read a line from the stream. } catch (Exception e) { throw new IOException ("Could not read from stream", e); } } }
• Handle errors via Application_Error event in global.asax. Application_Error event that gets fired every time an unhandled exception occurs in the web application.
protected void Application_Error(Object sender, EventArgs e) { Response.Write("Unexpected error occured ! <br>" & Server.GetLastError().Message); Response.End(); }
• Provide custom error pages for predefined IIS errors. This provides much better user experience by the use of custom error pages. The Page class has a property called as ErrorPage that can contain url of a HTML/ASPX page that will be displayed if any unhandled exception occurs in your code. Mywebform.ErrorPage="errpage.aspx" In the web.config file, turn on the customErrors mode as shown below: <customErrors mode="On" />
• Provide custom error pages for business errors. This you can customize error pages without going to IIS snap-in and also change them easily in future Modify <customErrors> section of web.config file.
<customErrors mode="On" defaultRedirect="errpage.aspx"> <error statusCode="404" redirect="filenotfound.aspx" /> </customErrors>
REGARDS, PRADEEP Y
|
| Author: Gaurav Arora 28 Oct 2008 | Member Level: Diamond Points : 2 |
Hi Paradeep,
A good article. Please note that: Whenever you post an article, it should be written descriptive with ease of language.
Please visit Guidelines for more information.
This is a good Article. But it attracts reader with its more stuff if it can be as :
Intrduction: Short Intro of article. Moto: Moto of Article History: History if any Scope: Scope of article Description: Detailed view of Article. Standard: Coding standard used in above.
Best of luck!
Thanks & regards, Gaurav Arora
|