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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:38:15+00:00 2026-05-20T12:38:15+00:00

First off, I think I know what’s going on, but I thought I’d bring

  • 0

First off, I think I know what’s going on, but I thought I’d bring this issue up here for some discussion and see if anyone has an “answer” to this other than what I’m thinking. Because, it doesn’t completely make sense to me.

What I found is that when creating a error log for exceptions, I was doing this and it wasn’t working:

catch( Exception ex )
{
   LogException( ex.Message );
   if ( !string.IsNullOrEmpty( ex.InnerException.Message ) )
   {
      LogInnerException( ex.InnerException.Message );
   }
}

and lo and behold, when I ran this I’d often get a NullReferenceException. Huh?

I’m checking for null, right?

now, I have to use this:

   if ( ex.InnerException != null && !string.IsNullOrEmpty( ex.InnerException.Message ) 

but that seems counter-intuitive and also counter productive. Because, heck, if I do this:

   if ( !string.IsNullOrEmpty( null ) )

That doesn’t give me any problems at all. And if ex.InnerException is null, then certainly ex.InnerException.Message is null, right?

Apparently not.

I wrote a complete console app that reproduces this. If you

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace stringisnullorempty
{
    class Program
    {
        static void Main( string[] args )
        {
            if ( !string.IsNullOrEmpty( null ) )
            {
                Console.WriteLine( "Ha ha ha, right...." );
            }

            MyBClass bClass = new MyBClass();
            bClass.BClassName = "Some B Class Name";
            if ( !string.IsNullOrEmpty( bClass.AClass.AString ) ) //<== Exception occurs here.
            {
                Console.WriteLine( bClass.AClass.AString );
            }
        }
    }

    public class MyAClass
    {
        private string aString;
        public string AString
        {
            get
            {
                return aString;
            }
            set
            {
                aString = value;
            }
        }

        private int aValue;
        public int AValue
        {
            get
            {
                return aValue;
            }
            set
            {
                aValue = value;
            }
        }

        public MyAClass() { }
    }

    public class MyBClass
    {
        private MyAClass aClass;
        public MyAClass AClass
        {
            get
            {
                return aClass;
            }
            set
            {
                aClass = value;
            }
        }

        private string bClassName;
        public string BClassName
        {
            get
            {
                return bClassName;
            }
            set
            {
                bClassName = value;
            }
        }
        public MyBClass() { }
    }
}

What I think is happening is that the code processes ex.InnerException.Message before trying to process the IsNullOrEmpty. Since ex.InnerException is null, we get an exception trying to access ex.InnerException.Message.

I’m wondering though, do I need the full check? Will the ex.InnerException != null be enough. If we have an inner exception, will we always have a message associated with it?

Thanks.

  • 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-20T12:38:15+00:00Added an answer on May 20, 2026 at 12:38 pm

    When you call ex.InnerException.Message, it’s not the message that is null, but rather then InnerException object.

    Think of it this way:

    string temp = ex.InnerException.Message
                  //               ^ the error is on this dot.
    if (string.IsNullOrEmpty(temp))
    {
        ...
    }
    

    To match exactly what you want to do, just use this:

    catch (Exception ex)  // PLEASE catch something more specific.
    {
       LogException(ex.Message);
       if (ex.InnerException != null)
       {
          LogInnerException(ex.InnerException.Message);
       }
    }
    

    In order to solve this problem, I have used this method in the past:

    public Exception GetInnermost(Exception ex)
    {
        while (ex.InnerException != null) ex = ex.InnerException;
        return ex;
    }

    ex.GetBaseException()
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, I know I can copy "this" on instantiation, but that doesn't work
Can anyone explain to me what is going on here? First off, I think
First off I know this is a duplicate question that is asked on here
first off I'm a noob to PHP but here is my problem. I am
First off, I'm using XCode 4.0.2. Okay, here is my issue. I can build
First off, full disclosure: This is going towards a uni assignment, so I don't
This starts off as an Android question but I think becomes a linux/cygwin question.
first off I'm going to say I don't know a whole lot about theory
First off, I don't have any knowledge about java/jquery/ajax or such, but I think
First off all I know: Premature optimization is the root of all evil But

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.