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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:06:34+00:00 2026-05-18T01:06:34+00:00

We have a property whose job is to look up a description. If the

  • 0

We have a property whose job is to look up a description. If the lookup fails it should show an empty string.

So we can code the property like this:

If foo.bar Is Not Nothing Then
  Return foo.bar.Description
Else
  Return String.Empty
End If

But that involves executing foo.bar twice, and if doing so is expensive, it’s probably better like this:

Dim b As bar = foo.bar
If b IsNot Nothing Then
  Return b.Description
Else
  Return String.Empty
End If

But really all we want to do is treat any kind of error as an empty description. So in some ways this is simpler:

Try
  Return foo.bar.Description
Catch e As NullReferenceException
  Return String.Empty
End Try

But are there any problems (performance, purity, other?) with just catching and ignoring the error?

You sometimes read it’s expensive to throw an exception but I am not sure whether the author means it’s expensive to build in exceptions using the Throw keyword (which I am not doing) or whether he means it’s expensive to allow exceptions to occur (as I would be doing).

  • 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-18T01:06:34+00:00Added an answer on May 18, 2026 at 1:06 am

    If would definitely test for Nothing instead of relying on exceptions here. You code indicates that the scenario where foo.bar is Nothing is an expected scenario, and not an exceptional one. That sort of gives the answer.

    Throwing an exception is a relatively expensive operation (from a performance perspective). This is the case regardless of wheter you are throwing it in your code, or if it is thrown in library code; it’s exactly the same operation. However, I would not keep from throwing exceptions for performance reasons unless I had a real, measured, critical business case.

    In my opinion this is mostly a matter of showing intent; by testing for Nothing and acting gracefully on that, your code expresses the fact that this is not a strange thing to happen.

    If you are worried about the performance in executing foo.bar twice, the first thing to do is to find out if that is really the case. If so, there are probably ways of solving that (your code sample already contains a suggestion).

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

Sidebar

Related Questions

I have a property whose value contains a $ . I'd like to use
I have a TabPanel whose HeaderText property I want to set using a code
I have a CurrentUser class with a static property whose value was stored in
I have a property of type uint on my entity. Something like: public class
If I have an entity derived from db.Expando I can write Dynamic property by
I currently have a NSArrayController whose content property is (programmatically) set to an NSMutableDictionary
I have a Data table whose primary key should be case sensitive. i.e. 'abc'
I have an ANT script which will have a property whose value could be
Folks, I have a custom WebPart (the ASP.Net variety) that has a property whose
I have a button whose content is binded with ViewModel's property called Test. <Button

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.