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

  • SEARCH
  • Home
  • 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 345549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:05:39+00:00 2026-05-12T11:05:39+00:00

I have this sort of format asp.net MVC View -> Service Layer -> Repository.

  • 0

I have this sort of format

asp.net MVC View -> Service Layer -> Repository.

So the view calls the service layer which has business/validation logic in it which in turns calls the Repository.

Now my service layer method usually has a bool return type so that I can return true if the database query has gone through good. Or if it failed. Then a generic message is shown to the user.

I of course will log the error with elmah. However I am not sure how I should get to this point.

Like right now my Repository has void return types for update,create,delete.

So say if an update fails should I have a try/catch in my repository that throws the error, Then my service layer catches it and does elmah signaling and returns false?

Or should I have these repository methods return a “bool”, try/catch the error in the repository and then return “true” or “false” to the service layer what in turn returns “true” or “false” to the view?

Exception handling still confuses me how handle the errors and when to throw and when to catch the error.

  • 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-05-12T11:05:39+00:00Added an answer on May 12, 2026 at 11:05 am

    The rule of thumb I always use is:

    • At low levels, throw when an operation cannot complete due to exceptional circumstances.
    • In middle layers, catch multiple exception types and rewrap in a single exception type.
    • Handle exceptions at the last responsible moment.
    • DOCUMENT!

    Here’s an example in pseudocode for a multi-layer ASP.NET MVC app (UI, Controller, Logic, Security, Repository):

    1. User clicks submit button.
    2. Controller action is executed and calls into the Logic (business) layer.
    3. Logic method calls into Security with the current User credentials
      • User is invalid
        • Security layer throws SecurityException
        • Logic layer catches, wraps in LogicException with a more generic error message
        • Controller catches LogicException, redirects to Error page.
      • User is valid and Security returns
    4. Logic layer calls into the Repository to complete action
      • Repository fails
        • Repository throws RepositoryException
        • Logic layer catches, wraps in LogicException with a more generic error message
        • Controller catches LogicException, redirects to Error page.
      • Repository succeeds
    5. Logic layer returns
    6. Controller redirects to the Success view.

    Notice, the Logic layer only throws a single exception type — LogicException. Any lower-level exceptions that bubble up are caught, wrapped in a new instance of LogicException, which is thrown. This gives us many advantages.

    First, the stack trace is accessible. Second, callers only have to deal with a single exception type rather than multiple exceptions. Third, technical exception messages can be massaged for display to users while still retaining the original exception messages. Lastly, only the code responsible for handling user input can truly know what the user’s intent was and determine what an appropriate response is when an operation fails. The Repository doesn’t know if the UI should display the error page or request the user try again with different values. The controller knows this.


    By the way, nothing says you can’t do this:

    try
    {
      var result = DoSomethingOhMyWhatIsTheReturnType();
    }
    catch(LogicException e)
    {
      if(e.InnerException is SqlException)
      {
        // handle sql exceptions
      }else if(e.InnerException is InvalidCastException)
      {
        // handle cast exceptions
      }
      // blah blah blah
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read over this which sort of gives an explanation of when you'd
Here's the situation... Site 1) ASP.NET MVC application for customers to login, view and
I have answered this sort of in a previous question but now I have
for (;;) { //Something to be done repeatedly } I have seen this sort
Scala programmer should have known that this sort of writing : class Person{ var
I'm doing a bit of coding, where I have to write this sort of
I have this array and I need to sort it, first by key_1 and
I have this following numpy matrix that I want to sort in ascending order
hello I have this code that use to sort a datatable. Dim sortingIndex As
Does anyone have any experience with this sort of thing? I'm talking about applets

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.