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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:16:24+00:00 2026-06-16T11:16:24+00:00

I like using implicit typing for almost everything because it’s clean and simple. However,

  • 0

I like using implicit typing for almost everything because it’s clean and simple. However, when I need to wrap a try…catch block around a single statement, I have to break the implicit typing in order to ensure the variable has a defined value. Here’s a contrived hypothetical example:

var s = "abc";

// I want to avoid explicit typing here
IQueryable<ABC> result = null;
try {
    result = GetData();
} catch (Exception ex) { }

if (result != null)
    return result.Single().MyProperty;
else
    return 0;

Is there a way I can call GetData() with exception handling, but without having to explicitly define the type of the result variable? Something like GetData().NullOnException()?

  • 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-16T11:16:26+00:00Added an answer on June 16, 2026 at 11:16 am

    This is a common problem. I recommend that you just stick with your existing solution.

    If you really want an alternative, here it is:

    static T NullOnException<T>(Func<T> producer) where T : class {
      try { return producer(); } catch { return null; } //please modify the catch!
    }
    
    //now call it
    var result = NullOnException(() => GetData());
    

    Please modify this to log the exception or restrict the catch to a concrete type. I do not endorse swallowing all exceptions.

    As this answer is being read a lot I want to point out that this implementation is just of demo-quality. In production code you probably should incorporate the suggestions given in the comments. Write yourself a robust, well-designed helper function that will serve you well for years.

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

Sidebar

Related Questions

In C#.NET, I like using HashSets because of their supposed O(1) time complexity for
Is there anything wrong with using an implicit operator like the following: //linqpad c#
I just read this post and it makes the case against implicit typing using
Like : using ::size_t; using ::fpos_t; using ::FILE; In fact it's a question inspired
I like using C-j to eval-last-sexp but paredit-mode (which I otherwise like) overrides this
I like using LINQ to SQL. The only problem is that I don't like
it seems like using $or with a sort does a full table scan and
Can you use 2 'using' statements like: using (SqlConnection ..) { using(SqlDataReader reader =
I have an entity and it seems like using DateTime as the Id would
for example I'm trying to get the data from database like: using (ExplorerDataContext context

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.