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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:32:31+00:00 2026-05-18T11:32:31+00:00

I have an odd question that I have always thought about, but could never

  • 0

I have an odd question that I have always thought about, but could never see a practical use for. I’m looking to see if there would be enough justification for this.

When is handling a null pointer/reference exception preferred over doing a null check? If at all.

This applies to any language that has to deal with null pointers/references which has exception handling features.

My usual response to this would be to perform a null check before doing anything with the pointer/reference. If non-null, continue like normal and use it. If null, handle the error or raise it.

i.e., (in C#)

string str = null;
if (str == null)
{
    // error!
}
else
{
    // do stuff
    int length = str.Length;
    // ...
}

However if we were not to do the check and just blindly use it, an exception would be raised.

string str = null;
int length = str.Length; // oops, NullReferenceException
// ...

Being an exception, we could certainly catch it so nothing is stopping us from doing this (or is there?):

string str = null;
try
{
    int length = str.Length; // oops, NullReferenceException
    // ...
}
catch (NullReferenceException ex)
{
    // but that's ok, we can handle it now
}

Now I admit, it’s not the cleanest code, but it’s no less working code and I wouldn’t do this normally. But is there a design pattern or something where doing this is useful? Perhaps more useful than doing the straight up, null check beforehand.

The only cases where I can imagine this might be useful is in a multi-threaded environment where an unprotected shared variable gets set to null too soon. But how often does that happen? Good code that protects the variables wouldn’t have that problem. Or possibly if one was writing a debugger and wanted the exception to be thrown explicitly only to wrap it or whatnot. Maybe an unseen performance benefit or removes the need for other things in the code?

I might have answered some of my questions there but is there any other use to doing this? I’m not looking for, “do this just because we can” kinds of examples or just poorly written code, but practical uses for it. Though I’ll be ok with, “there’s no practical use for it, do the check.”

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

    The problem is that all null pointer exceptions look alike. Any accounting that could be added to indicate which name tripped the exception can’t be any more efficient than just checking for null in the first place.

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

Sidebar

Related Questions

I have a question that may sound odd, but being somewhat of a newbie,
This might be an odd question, but I have form in MVC3 that posts
So this might be an odd question, but I have a C# program that
I guess this is kind of an odd question but I have tried setting
An odd question this will be, but here goes. I have a page on
Kind of an odd question, but I have a game I am making for
I know that question sounds a little odd but i will try to explain
I have a question that most of you might find a little odd. I
I have an odd question that im not sure has been asked/answered, and im
I'm not finding any question specifically with the odd problem I have. Basically I've

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.