0

I am a new developer. I am working on an ASP .NET Core project with C # and using "try catch" for error handling. However, my project is getting heavy due to too many "try catch" and also the code gets a little confusing.

My question is whether it would be possible to create a "global class" or some other way to handle errors?

  • 4
    Are you sure you are not abusing try...catches? There shouldn't be too many of them... – Sweeper Dec 18 '19 at 00:51
  • 1
    See [Why should I not wrap every block in “try”-“catch”?](https://stackoverflow.com/q/2737328/205233) – Filburt Dec 18 '19 at 00:55
  • 1
    If your project is *"getting heavy due to too many "try catch" and also the code gets a little confusing."*, then most likely you're doing it wrong. `try/catch` is for *exceptional* situations. Most of the time (especially in low-level methods) you should just let the exceptions bubble up to the caller who eventually knows how to handle the exception and take some corrective action. – Rufus L Dec 18 '19 at 00:55
  • Also, `public` is the widest scope a class can have. What do you mean by "global"? – Rufus L Dec 18 '19 at 00:58
  • Yes you can remove some of those try/catch blocks, for example if you write an extension method to catch every ex in a timer Elapsed. But here's what you should do : only catch errors if its really necessary, for Null Pointer etc you don't need a try / catch. Instead add all the global error handlers, that way you can fix most bugs instead of catching them everywhere. – Charles Dec 18 '19 at 01:04

2 Answers2

1

Yes you can.

Your question has already been answered in some capacity here

Here are four examples using different project types:

ASP.NET Core MVC by implementing a custom ExceptionFilterAttribute:

public class MyExceptionFilterAttribute : ExceptionFilterAttribute
{
    public override void OnException(ExceptionContext context)
    {
        Exception ex = context.Exception;
        Logger.Error(ex);
    }
}

[Route("api/[controller]")]
[MyExceptionFilter]
public class HomeController : Controller
{
    // Action methods
}

ASP.NET MVC by overriding OnException in your controller:

protected override void OnException(ExceptionContext context)
{
    if (context.ExceptionHandled)
    {
        return;
    }
    Logger.Error(context.Exception);
    context.Result = RedirectToAction(MVC.Error.Application());
    context.ExceptionHandled = true;
}

Windows Application by subscribing to UnhandledException event:

static void Main()
{
    AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    Console.WriteLine(e.ExceptionObject.ToString());
}

Web Forms by adding Application_Error handler in global.asax:

protected void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
    Logger.Error(ex);
}
mr.coffee
  • 962
  • 8
  • 22
-4

Absolutely the answer is yes if I. However it depends on what kind of projects you are doing. you can catch the unhandled exception in the global.cs by overriding method or create an Attribute class to catch exception.

YuZhao
  • 43
  • 1
  • 2