Home > Enterprise >  How to handle SystemInvalidOperationException Sequence contains no elements'?
How to handle SystemInvalidOperationException Sequence contains no elements'?

Time:09-30

private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            string rootPath = Environment.ExpandEnvironmentVariables(@"d:\downloads\main");

            DirectoryInfo directory = new DirectoryInfo(rootPath).GetDirectories()
                .OrderByDescending(d => d.CreationTimeUtc)
                .First();

            Editor editor = new Editor();
            editor.Show();
        }

the exception happens on the line :

DirectoryInfo directory = new DirectoryInfo(rootPath).GetDirectories()
                    .OrderByDescending(d => d.CreationTimeUtc)
                    .First();

should i check against something in that line that is not empty or null? it happens when the rootPath is empty.

CodePudding user response:

You should do more checking. The issue starts from the DirectoryInfo model.As you mentioned you have to check if the path is not null or empty and further you also need check if the directory actually exists.

string path = "";
        if (string.IsNullOrWhiteSpace(path))
        {
            //do something
        }

        DirectoryInfo directory = new DirectoryInfo(path);
        if (!directory.Exists)
        {
            //do something
        }

CodePudding user response:

Here is how to handle exceptions in c#: https://www.c-sharpcorner.com/article/csharp-try-catch/

In your case you could use FirstOrDefault() instead of First, which is then able to return null

You could make DirectoryInfo nullable by adding ? after the type: e.g.

DirectoryInfo? directoryinfo = ...

then do a nullcheck and exclude the editor and or show an error message.

example: if(!(directioninfo is null)) or if(directoryinfo != null)

note that the "is not" statement only works starting at lang version 7.0

Here just for you:

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                string rootPath = Environment.ExpandEnvironmentVariables(@"d:\downloads\main");

                DirectoryInfo? directory = new DirectoryInfo(rootPath).GetDirectories()
                    .OrderByDescending(d => d.CreationTimeUtc)
                    .FirstOrDefault();

                if (directory is null)
                {
                    if (string.IsNullOrEmpty(rootPath))
                        throw new Exception("Root path was empty.");
                    throw new DirectoryNotFoundException("No directories found on path")
                }

                Editor editor = new Editor();
                editor.Show();
            }catch(DirectoryNotFoundException dirEx)
            {
                MessageBox.Show(dirEx.Message);
                return;
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }
  • Related