locked
best place to catch errors RRS feed

  • Question

  • User-1025832227 posted

    i have just created an n-layer application with the following layers:

    - BusinessObjects
    - DataAccess
    - BusinessLogic

    What/how is the best way to catch errors (at no matter which level they occur, so that I can trap them and log them to a database)?

    Sunday, January 4, 2009 9:36 AM

Answers

  • User-158764254 posted

    i generally wait until i'm prepared to deal with the error before i add a Catch.  This means that [for me] i do not catch down in the lower layers of my app.  Instead i let the error bubble so that the calling code will know that an error has actually occurred.  If you catch too early, then the calling code will not necessarily know that an error occurred and so wouldnt be able to properly communicate the error to the user of the app. Presenting an error message to the user is a function of your UI layer so the UI must have access to the error in some fashion. So i'm more apt to throw errors from my business layer and data layer and then catch those errors up in the web app itself - either in the page or in a global error handler.

    I use the EMAB to log the errors and send exception emails

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 4, 2009 10:53 AM
  • User-821857111 posted

    Catch them where they happen then use a logging utility to record them in a database/send an email etc.  Something like this should help: http://msdn.microsoft.com/en-us/library/dd203116.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 4, 2009 10:59 AM
  • User-481631678 posted

    I never recommend to put Try/Catch in all places of your code.

    Generally there are two types of error:-

    1- Fatal Errors and your application cant proceed after that point such as Database Unavailability (if you are developing DB related application)

    These Types of Errors should be handled in webconfig --> CustomError Tag . Define a customized error page which logs the error and display user a understandable version of the error.

    2- Non-Fatal Errors such as application is unable delete from db due to foreign key constraints.

    These types of errors should be handled from aspx where call is initiated and display user an appropriate message on page.

     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 5, 2009 3:30 AM
  • User-1935625286 posted

     I am with Zeeshan about the style of displaying errors to the user.

     

    To add:

     

    1. You can have a framework like Microsoft's Enterprise Library and then use its classes to trap and log errors etc.

    OR

    2. You can use your own Exception logging class that you can instantiate globally and trap/log errors whenever/whereever they occur through it [your own Exception logging class].

     

    But I can't keep myself from recommending you to use the LINQ to Entities. Its just AMAZING. Thanks Microsoft.

     

    Check here:

    http://msdn.microsoft.com/en-us/library/bb386992.aspx

     

    Good luck.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 5, 2009 3:58 AM

All replies

  • User-158764254 posted

    i generally wait until i'm prepared to deal with the error before i add a Catch.  This means that [for me] i do not catch down in the lower layers of my app.  Instead i let the error bubble so that the calling code will know that an error has actually occurred.  If you catch too early, then the calling code will not necessarily know that an error occurred and so wouldnt be able to properly communicate the error to the user of the app. Presenting an error message to the user is a function of your UI layer so the UI must have access to the error in some fashion. So i'm more apt to throw errors from my business layer and data layer and then catch those errors up in the web app itself - either in the page or in a global error handler.

    I use the EMAB to log the errors and send exception emails

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 4, 2009 10:53 AM
  • User874596136 posted

    It depends on the errors. Some errors will be handler in the tier they were raised (and therefore hidden from other tiers), but others must make the way to the top layers.

    I'd sugest this kind of approach:
    - Handle error on it's on tier (if needed) and log it if needed
    - Wrap the error based on tiers. Needed because there are different abstraction on different tiers. BusinessLogic layer don't need to know, that there were some SQL exceptions for example, it needs to know only that something went wrong with DataAccess.
    - Throw the error to the upper layers (if needed)

    Finally, you should define some global error hander (ie using Application_Error event handler on Global.asax) to catch any unexpected exceptions and to display to the user user-friendly message.

    Sunday, January 4, 2009 10:53 AM
  • User-821857111 posted

    Catch them where they happen then use a logging utility to record them in a database/send an email etc.  Something like this should help: http://msdn.microsoft.com/en-us/library/dd203116.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 4, 2009 10:59 AM
  • User-1959498717 posted

    try this link here: http://msdn.microsoft.com/en-us/library/dd140113.aspx

    Sunday, January 4, 2009 12:20 PM
  • User-481631678 posted

    I never recommend to put Try/Catch in all places of your code.

    Generally there are two types of error:-

    1- Fatal Errors and your application cant proceed after that point such as Database Unavailability (if you are developing DB related application)

    These Types of Errors should be handled in webconfig --> CustomError Tag . Define a customized error page which logs the error and display user a understandable version of the error.

    2- Non-Fatal Errors such as application is unable delete from db due to foreign key constraints.

    These types of errors should be handled from aspx where call is initiated and display user an appropriate message on page.

     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 5, 2009 3:30 AM
  • User-1935625286 posted

     I am with Zeeshan about the style of displaying errors to the user.

     

    To add:

     

    1. You can have a framework like Microsoft's Enterprise Library and then use its classes to trap and log errors etc.

    OR

    2. You can use your own Exception logging class that you can instantiate globally and trap/log errors whenever/whereever they occur through it [your own Exception logging class].

     

    But I can't keep myself from recommending you to use the LINQ to Entities. Its just AMAZING. Thanks Microsoft.

     

    Check here:

    http://msdn.microsoft.com/en-us/library/bb386992.aspx

     

    Good luck.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 5, 2009 3:58 AM