I used Delphi language a few years ago and there was a centralized error handler object where any error caused within any method of my system was triggered to this "centralized object" and
I could for example write the error to my database, redirect the
user to an error page, login page, etc.
Is there something like this inside Visual Studio or is it possible to create a class like this where it could be
triggered
if any method of my system gets an error, or do I really need to repeatedly create the clauses: try, catch, finally inside each method of my system individually?
Depends on the framework. For ASP.NET, unhandled exceptions bubble up to Application_Error in the Global.asax. You can write code to log exception in this event.
Is there something like this inside Visual Studio or is it possible to create a class like this where it could be
triggered
if any method of my system gets an error, or do I really need to repeatedly create the clauses: try, catch, finally inside each method of my system individually?
You can add error handling at the application, page, and code levels. Check this
link for few examples:
Application-Level Error Handling: you can use Application_Error event in Global.asax to catch unhanded exceptions..
// IN Global.asax
public class MyApplication : System.Web.HttpApplication
{
protected void Application_Error(object sender, EventArgs e)
{
Exception unException = Server.GetLastError();
if (unException != null)
{
if (exc is HttpUnhandledException)
{
// Pass the error on to the error page.
Server.Transfer("ErrorPage.aspx?handler=Application_Error%20-%20Global.asax", true);
}
//log error to DB ??
}
}
}
Member
5 Points
8 Posts
Centralized error handler object?
Jan 16, 2020 03:32 PM|Rorion|LINK
I used Delphi language a few years ago and there was a centralized error handler object where any error caused within any method of my system was triggered to this "centralized object" and I could for example write the error to my database, redirect the user to an error page, login page, etc.
Is there something like this inside Visual Studio or is it possible to create a class like this where it could be triggered if any method of my system gets an error, or do I really need to repeatedly create the clauses: try, catch, finally inside each method of my system individually?
What is the best approach in this case?
All-Star
53051 Points
23634 Posts
Re: Centralized error handler object?
Jan 16, 2020 03:55 PM|mgebhard|LINK
Depends on the framework. For ASP.NET, unhandled exceptions bubble up to Application_Error in the Global.asax. You can write code to log exception in this event.
https://odetocode.com/articles/69.aspx
This pattern works for a common logger.
Exception in ASP.NET Core are handled in middleware.
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-3.1
All-Star
29523 Points
6026 Posts
Re: Centralized error handler object?
Jan 16, 2020 04:02 PM|budugu|LINK
You can add error handling at the application, page, and code levels. Check this link for few examples:
Application-Level Error Handling: you can use Application_Error event in Global.asax to catch unhanded exceptions..
"Don't be afraid to be wrong; otherwise you'll never be right."
Contributor
4953 Points
4207 Posts
Re: Centralized error handler object?
Jan 17, 2020 03:49 PM|DA924|LINK
The link gives all the ways global exception handling can be implemented based on project type
https://stackify.com/csharp-catch-all-exceptions/
Member
5 Points
8 Posts
Re: Centralized error handler object?
Jan 17, 2020 10:48 PM|Rorion|LINK
Great!
Member
5 Points
8 Posts
Re: Centralized error handler object?
Jan 17, 2020 10:49 PM|Rorion|LINK
Thanks!