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 7678029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:33:07+00:00 2026-05-31T17:33:07+00:00

Should all linq queries which return an entity be surrounded with a try catch?

  • 0

Should all linq queries which return an entity be surrounded with a try catch?

For example:

phase.Container = containers.Where(cont => cont.ContainerId == phase.ContainerId).SingleOrDefault();

This will not cause the application to crash, but will cause an exception if the Container object is not present.

EDIT

Even doing this will still cause the view to have an exception:

bool schedValid = false;
foreach (var s in schedules)
{
 if (s.ScheduleId == phase.ScheduleId) schedValid = true;
}
if (schedValid)
{
 phase.Schedule = schedules.SingleOrDefault(sc => sc.ScheduleId == phase.ScheduleId);
}
else
{
 phase.Schedule = null;
}

Edit 2

The reason for all this effort is that if a parent object is deleted then the children objects which contain data should still be allowed to be viewed even though their parent is gone. An argument can be made that the parent object should not be allowed to be deleted. Fair enough, but there is no point of keeping it around when the children have become more relevant than the parent and for whatever reason the parent no longer is required to be kept. At a later time, the children may be re-assigned to a different parent object.

  • 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-31T17:33:08+00:00Added an answer on May 31, 2026 at 5:33 pm

    This is a response to the question in the title, not the specific example given in the body of the question.

    “It depends”

    What are you going to do when the exception happens? Can you reasonably respond a database error? If not, don’t catch the exception. If you can reasonable respond in such a way that allows the rest of the application to continue execution, then catch the exception.

    See this great post from Eric Lippert for more details about exception handling.
    http://blogs.msdn.com/b/ericlippert/archive/2008/09/10/vexing-exceptions.aspx

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

Sidebar

Related Questions

When using compiled queries in entity framework (or linq-to-sql) in combination with SQL Server,
Should all structs and classes be declared in the header file? If I declare
I have a few links that should all open in the same window or
Are primitives worth keeping? Should all the deprecated stuff be deleted? Do we need
When you log out of a web app, should ALL your session be logged
As Jeff Atwood asked : "What’s your logging philosophy? Should all code be littered
The script below should open all the files inside the folder 'pruebaba' recursively but
I know that I should put all the html elements in body tag, but
How can I specify a td tag should span all columns (when the exact
The following line in my C program should provided All/Group/Owner read and write permissions

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.