Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace try...catch block in c#

Tags:

c#

exception

web

Is there any possibility/code other than using try...catch block for exception handling and further logging. We are developing a Web application with couple of screen. Client asked us to not to put try...catch blocks in each functions/methods to handle errors. Please suggest.

Thanks

like image 721
Yogesh Avatar asked Jul 11 '26 05:07

Yogesh


2 Answers

We are developing a Web application with couple of screen

Although the requirement of you client does not make much sense for me but you can handle the unhandled exception using Application_Error event in global.asax

void Application_Error(object sender, EventArgs e)
{
     //Your logging code goes here
}

How to: Handle Application-Level Errors

This code example shows how to create an error handler in the Global.asax file that will catch all unhandled ASP.NET errors while processing a request — in other words, all the errors that are not caught with a Try/Catch block or in a page-level error handler. In the example, the handler transfers control to a generic error page named GenericErrorPage.aspx, which interprets the error and displays an appropriate message.

As a additional Note for Desktop application like WinForms or Window Services you can using AppDomain.CurrentDomain.UnhandledException or Application.ThreadException etc.

like image 181
Adil Avatar answered Jul 14 '26 09:07

Adil


You can use Application_Error in global.asax, This method handles the error for all the pages in the application.

for specific page for example default.aspx, you can implementing Page_Error