Exception Handling and Logging Best Way

Rate It (1)

Last post 07-29-2009 11:37 AM by vrakesh. 5 replies.

Sort Posts:

  • Exception Handling and Logging Best Way

    06-30-2009, 4:31 AM
    • Member
      31 point Member
    • sarfraz_khari
    • Member since 05-15-2009, 1:00 PM
    • Posts 7

    I am writing a desktop application that has three layers, where in each layer there is the possibility of Exceptions being occured. Where it is best way to handle an exception if it is raised in a lower layer, such as the data layer or Business Layer? For example, I have the following layers.

    Note: I am using Enterprise Library 4.1for exception and logging block, library practice allows me to handle it at any layer, Now question is that at which layer it should be handled.

    UI Layer (WinForm) -> Business Layer (BLL) -> Data Layer (DLL).

    At Three Layers below are possibilities;

    UI layer – Reference(.dll) of BLL is added in this layer.

    UI layer exception definitely should be handled at UI layer and should be logged using logging block.

    Business LAYER – this is not a web service, these are wrapper function with some business checks. Reference(.dll) of Date Layer is added in this layer.

    1. One option is that bubble it to UI layer and where it will be handled as others are handled and logged. (Benefit of this option is that I have create only One Exception Configuration as well as one Exception Handler; where as in below case I have to Place Exception Handler at home Common Layer, which will be added in BLL as well as in DLL.)
    2. Other option is that I logged it at the same level where it is raised and ReThrow it to UI layer where it will be handled as per Exception Policy.

    Dater Layer – This layer handles direct database calls using Data Block.

    1. One option is that bubble it towards end point that is UI layer and where it will be handled as others are handled and logged.
    2. Other option is that I logged it at the same level where it is raised and ReThrow it to UI layer where it will be handled as per Exception Policy.
    Have a good life.
  • Re: Exception Handling and Logging Best Way

    06-30-2009, 4:45 AM
    • Contributor
      6,106 point Contributor
    • sreejukg
    • Member since 10-31-2004, 7:33 AM
    • Bahrain
    • Posts 1,029

    I prefer to raise all the exceptions from the business layer... The methods provided by the busines layer may throw error. Let the UI application log the error, So you can handle the same error in multiple ways in multiple situations. for e.g. some place you will log the error to db, in some place you will do logging and show alert to the user, in some place you will send the error as email.. so it will be better if you dedicate UI for logging,... and business layer to raise exceptions..

    The real voyage of discovery consists not in making new landscapes, but in having new eyes
  • Re: Exception Handling and Logging Best Way

    06-30-2009, 2:21 PM
    Answer
    • Contributor
      5,726 point Contributor
    • atconway
    • Member since 09-24-2007, 5:20 PM
    • Florida U.S.A
    • Posts 1,194

    I prefer to let all exceptions from the DLL and BLL bubble up to the UI and assign the topmost caller the responsibility of handling the exception.  I typically do not like my DLL or BLL to have to understand anything about my UI, so therefore I feel that those (2) components need to allow the exception to bubble up to the layer of the application responsible for orchastrating the application (most events are initiated by user or other interaction at the UI layer; obviously talking about a web app here and not a service).

    The main caller then can catch the exception and then log it, write to the event log, display it to the user (in a meaningful way), etc.

    There are times that are appropriate to handle and catch exceptions in the BLL or DLL, so it is not a hard and set rule.  If you can catch the exception of a specific type in say the BLL, and take action on it to fix the situation, then this would be a good spot to handle them too.  For example, you handle a connection timeout and then take action to attempt recalling the source 1 more time before allowing the exception to bubble up.  Another instance is if an argument value is passed in that is not valid and throws an exception. However the business rules dictate that another default value can be used safely, so you catch the exception, handle it, and continue on processing.

    Of the most basic exception handling for logging though I reccomend you let those bubble up to be handled.

    Hope this helps!

    Thank you,   >[Blog]<

    "The best thing about a boolean is even if you are wrong, you are only off by a bit." :D
    -anonymous

  • Re: Exception Handling and Logging Best Way

    06-30-2009, 2:43 PM
    • Participant
      893 point Participant
    • infosolvex
    • Member since 09-11-2006, 7:42 PM
    • Posts 134

    I would use the approach where you log based on the need rather than trying to impliment logging in just one layer, as you will always find exception to any rule where you adhere logging to any one layer.

    Ritesh Ramesh

    p.s. Please click "Mark as Answer" if you think this post answered your question
  • Re: Exception Handling and Logging Best Way

    07-03-2009, 4:20 PM
    Answer
    • All-Star
      17,710 point All-Star
    • vivek_iit
    • Member since 06-18-2006, 6:13 PM
    • New Delhi
    • Posts 3,171
    • TrustedFriends-MVPs

    sarfraz_khari:
    Where it is best way to handle an exception if it is raised in a lower layer, such as the data layer or Business Layer?

    Think it like this: in most cases the DAL layer acts "dumb", it has not "logic" in itself. So it is more or less like a utility layer and hence it should not handle any exceptions. I mostly throw exceptions to the higher layers above DAL, like the BL Layer, and process it there (log it too).

    The BL should send another customized expception to the layer above it (after logging that exception and maybe wrapping it up in an easy to understand language). The UI layer can have its own logger and it should show a meaningful exception message to the end user.

  • Re: Exception Handling and Logging Best Way

    07-29-2009, 11:37 AM
    • Member
      4 point Member
    • vrakesh
    • Member since 04-21-2009, 4:48 PM
    • Posts 4

     Hey, thanks for question  and all answers. This is really where I'm struggling to make a decision.

    I have the same scenario in my app. UI layer is on different server and BAL&DAL are on one server. Since UI layer is in DMZ ,  I can not directly access that server to view log files. Its hassle to remote into one more virtual sever and login into the DMZ server and look into the Log files.

    To log all activities to a text file, User interactions in UI and method calls in BAL&DAL what is best method?

    Since there are 2 servers, there would be 2 log files for each user interaction. one is on UI layer and other one is in BAL&DAL layer. I can write all activities in BAL&DAL layer log file, but If there is any error in UI before even request goes to BAL then It wont be logged. How to achieve this? If question is not clear, please let me know, I will try to write in more detail..

    Thanks,

    Rakesh

     

     

Page 1 of 1 (6 items)