There are three different ways to handle exceptions in ASP.NET. These are
a) Try/catch/finally block: You can enclose your codes in Try/Catch/Finally block. You can catch all exceptions in the catch block. The third part of this block is finally. It is executed irrespective of the fact that an exception has been raised.
b) Using Events like Page_Error and Application_Error:
• Page_Error: This is page event and is raised when any unhandled exception occur in the page.
• Application_Error: This is application event and is raised for all unhandled exceptions in the ASP.NET application and is implemented in global.asax
c) Using Custom error page: The <customErrors> section in web.config has two attributes that affect what error page is shown: defaultRedirect and mode. The defaultRedirect attribute is optional. If provided, it specifies the URL of the custom error page and indicates that the custom error page is shown instead of the Runtime Error. The mode attribute is required and accepts three values: On, Off, and RemoteOnly. These values have the following behavior:
On - indicates that the custom error page or the Runtime Error is shown to all visitors, regardless of whether they are local or remote.
Off - specifies that the Exception Detail is displayed to all visitors, regardless of whether they are local or remote.
RemoteOnly - indicates that the custom error page or the Runtime Error that is shown to remote visitors, while the Exception Detail is shown to local visitors.
Asked In: Many Interviews |