3

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

DrKoch
  • 9,556
  • 2
  • 34
  • 43
Yogesh
  • 1,206
  • 4
  • 22
  • 50
  • Is there a reason your client does not want `try` blocks? There's no other way I can think of to handle errors. – AStopher Mar 11 '15 at 09:16
  • Well no, you wouldn't put it in every method. You should generally have more centralized exception handling - and ASP.NET typically does this for you, allowing you to register an error page. It really depends on what exceptions you're trying to handle, and what you're trying to do if they occur. (For example, if you're making a web service request and want to retry, you may well want a try/catch somewhere for that, in order to stop your whole web page request from failing...) – Jon Skeet Mar 11 '15 at 09:16
  • possible duplicate of [.NET - What's the best way to implement a "catch all exceptions handler"](http://stackoverflow.com/questions/219594/net-whats-the-best-way-to-implement-a-catch-all-exceptions-handler) – Paddy Mar 11 '15 at 09:16
  • Very strong odds that your client is very concerned that bugs in your program become *his* problem instead of yours. He is right, try/catch in every method is a very, very bad practice. – Hans Passant Mar 11 '15 at 09:21

2 Answers2

3

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.

Adil
  • 146,340
  • 25
  • 209
  • 204
1

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

Karthikeyan
  • 381
  • 8
  • 19