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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:19:47+00:00 2026-05-21T17:19:47+00:00

To start, I realized there is probably a better way to do this, and

  • 0

To start, I realized there is probably a better way to do this, and rather than throwing an exception I should just handle the condition better. That being said, I ran into some unexpected behavior and I’m more curious why this is happening than using it my application.

In a method, I am attempting to access a file provided by the user. At he beginning of the method, I am checking to make sure that the path to the file is not Null or String.Empty and throwing an exception if it is. When I was testing, I am finding that the exception is thrown regardless of the condition. Is this normal behavior, or am i missing something?

public static XElement Foo(String path)
{
    if (String.IsNullOrEmpty(path))
    {
       throw new ArgumentNullException(); // this exception is thrown  
                                          // regardless of the value of 'path'
    }


    // code to open and parse file
    // returns XElement
}

UPDATE:

In my testing scenerio, the calling method is just sending a default path that i hard coded for the test. I have not completed the UI, so the code for the user to define the path is not complete.

private const string c_fooPath = "C:\\test\\text.txt"

public void CallingFoo()
{
    var xml = Foo(c_fooPath)

    // some code

}

UPDATE #2:

Just to mention some of my other tests i have tried. I have tried

if (String.IsNullOrEmpty(path))
{
    Console.WriteLine("testing")       // this line is skipped when my condition is
                                       // false but runs when  i force it to be true

    throw new ArgumentNullException(); // this exception is thrown  
                                       // regardless of the value of 'path'
}

if (false)
{
    throw new ArgumentNullException(); // the exception is not thrown here - the only
                                       // condition i have found so far.
}

public static XElement Foo(String path)
{
    path = "test";

    if (String.IsNullOrEmpty(path))
    {
       throw new ArgumentNullException(); // exception is still thrown  
    }


    // code to open and parse file
    // returns XElement
}
  • 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-21T17:19:48+00:00Added an answer on May 21, 2026 at 5:19 pm

    I’ve given your code a quick test and it works as expected, i. e. throws only the exception if the string is null or empty. Debug your code to see if the given path actually has content and really isn’t empty or null. Maybe there could also something wrong in the caller of the function.

    My testcode (returns void instead of your XElement):

    class Program {
        static void Main(string[] args) {
            Foo("test");
        }
    
        public static void Foo(String path) {
            if (String.IsNullOrEmpty(path)) {
                throw new ArgumentNullException();
            }
        }
    }
    

    You could also try Convert.ToString((object)stringVar) == "" instead of String.IsNullOrEmpty.

    Update: Maybe this or this helps.

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

Sidebar

Related Questions

I just realized that when i start a task from within a task and
This is a rather simple example and probably wouldn't make much of a difference
I start Firefox (Linux) via command line using a cron-job. When there is no
I start a process in c# like this: Process p= new Process(); p.StartInfo.FileName =
I start dicoverig C# today ... as you know there is some difuculties using
Is there a way to run a regexp-string replace on the current line in
I realize I am probably doing this completely wrong, but here is my issue...
I realize that this is probably not the smartest thing to do with regular
Start with a series of MS Office extensions built in C++ as COM objects.
Ramaze.start :port => 80 If my understanding is correct, the line above is a

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.