Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7731129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:24:12+00:00 2026-06-01T06:24:12+00:00

Before you run off and google, I have read lots of articles, posts, and

  • 0

Before you run off and google, I have read lots of articles, posts, and comments on Exception handling, but I am still stuck on these specific points.

Given the example below how would you deal with the following scenarios?

  1. If an exception occurs in the DAL layer and is wrapped in a custom exception to provide more information and to better categorize it. How do I log it?

If I log it as it occurs in the DAL layer it will be logged again in the global handler (using elmah). I could let it propogate, but what happens if the ServiceLayer needs to translate that exception into a more user-friendly message for a user or possible for transactional purposes (think rollback)? I will lose information gathered in the DAL exception (the message anyway, not necessarily the stacktrace).

 // UI
        public static GetUser(int userId)
        {
            // Should I do validation here or in service layer
            try
            {
               IUserService s = new UserService(userId);
               s.GetUser(userId);
            } 
            catch(ServiceLayerException ex)
            {
                // ex.Message displayed to user
            }

        }


        // Service layer
        public User GetUser(int userId)
        {           
            try
            {
                return repo.GetUser(userId);
            }
            catch(DALException ex)
            {
                // user-friendly message displayed to user
                throw new ServiceLayerException("User does not exist");
            }
        }



        // DAL
        public User GetUser(int userId)
        {
            try
            {
                // Query for user, if fails throw DALException
                return userId;
            }
            catch (SqlException ex)
            {
                throw new DALException("Could not retrieve user with userId " + userId.ToString());
            }

        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:24:14+00:00Added an answer on June 1, 2026 at 6:24 am

    Personally I prefer to Log at the coalface – where the exception actually happened, so in the first case I would log in the DAL. The Bal (Service Layer) can handle the error if we do not want to propogate the error, or allow elmah to log it (which is not a bad thing as it will show on the error page and help with tracking the exception).
    The service layer can also pass the DAL error as an inner exception so that it is accessable during/after propgation if required – and exception iteslf can be mutated as required between layers. The user will generally not want such info, and just receives the usual dumbed down message.
    It is also possible to use events to collect exception data (with specialised event arguements) and just pass back boolean methods returns, this can be more controllable and tailorable – though this then puts the onus on you to propogate or not, and it is likely to only be used for “expected” exceptions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to have setup/teardown code automagically run before/after each test? Something
I want to have a Mercurial hook that will run before committing a transaction
I have maven-plagin and need to run goal, that should automatically run before plugin.
Before the performance people tear my head off: yes, I have done profiling before
I am working with a XML driven CMS, and before I run off and
I have to SET ANSI_NULLS and few other database options before I run some
This is a problem I have run into before and I have yet to
I want a validation to run before a record gets updated. I know of
I'm trying something new and trying to run before I can crawl. I'm using
I'm attempting to add a mirgration before I run my project. I get the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.