Skip to content

Exceptions running the finished app. #46915

Open
@jvsca

Description

@jvsca

Type of issue

Code doesn't work

Description

  1. In finished app, several Possible null:

    static int Main(string[] args)
    {
        Option<FileInfo> fileOption = new("--file")
        {
            Description = "An option whose argument is parsed as a FileInfo",
            Required = true,
            DefaultValueFactory = result =>
            {
                if (result.Tokens.Count == 0)
                {
                    return new FileInfo("sampleQuotes.txt");
    
                }
                string filePath = result.Tokens.Single().Value;
                if (!File.Exists(filePath))
                {
                    result.AddError("File does not exist");
                    return null;                        // ⚠️ CS8603: Possible null reference return.
                }
                else
                {
                    return new FileInfo(filePath);
                }
            }
        };
    
        ...
    
        readCommand.SetAction(parseResult => ReadFile(
            parseResult.GetValue(fileOption),           // ⚠️ CS8603: Possible null argument for parameter 'file' in 'void Program.ReadFile(FileInfo file, ...)'.
            parseResult.GetValue(delayOption),
            parseResult.GetValue(fgcolorOption),
            parseResult.GetValue(lightModeOption)));
    
        deleteCommand.SetAction(parseResult => DeleteFromFile(
            parseResult.GetValue(fileOption),           // ⚠️ CS8603: Possible null argument for parameter 'file' in 'void Program.DeleteFromFile(FileInfo file, ...)'.
            parseResult.GetValue(searchTermsOption)));  // ⚠️ CS8603: Possible null argument for parameter 'searchTerms' in 'void Program.DeleteFromFile(FileInfo file, ...)'.
    
        addCommand.SetAction(parseResult => AddToFile(
            parseResult.GetValue(fileOption),           // ⚠️ CS8603: Possible null argument for parameter 'file' in 'void Program.AddToFile(FileInfo file, ...)'.
            parseResult.GetValue(quoteArgument),        // ⚠️ CS8603: Possible null argument for parameter 'quote' in 'void Program.AddToFile(FileInfo file, ...)'.
            parseResult.GetValue(bylineArgument))       // ⚠️ CS8603: Possible null argument for parameter 'byline' in 'void Program.AddToFile(FileInfo file, ...)'.
            );
  2. File Not Found Exception in ReadFile():

    scl quotes read --file nofile
    internal static void ReadFile(FileInfo file, int delay, ConsoleColor fgColor, bool lightMode)
    {
        Console.BackgroundColor = lightMode ? ConsoleColor.White : ConsoleColor.Black;
        Console.ForegroundColor = fgColor;
        foreach (string line in File.ReadLines(file.FullName)) // ❌ System.IO.FileNotFoundException: 'Could not find the file '...\nofile'.'
        {
            Console.WriteLine(line);
            Thread.Sleep(TimeSpan.FromMilliseconds(delay * line.Length));
        }
    }

    Note:

     fileOption.DefaultValueFactory is never executed.
    
  3. IO Exception:

    scl quotes delete --search-terms David "You can do" Antoine "Perfection is achieved"
    internal static void DeleteFromFile(FileInfo file, string[] searchTerms)
    {
        Console.WriteLine("Deleting from file");
    
        var lines = File.ReadLines(file.FullName).Where(line => searchTerms.All(s => !line.Contains(s))); // ❌ System.IO.IOException: 'The process cannot access the file '...\sampleQuotes.txt' because it is being used by another process.'
        File.WriteAllLines(file.FullName, lines);
    }

    Solution:

        File.WriteAllLines(file.FullName, lines.ToList());

Page URL

https://learn.microsoft.com/en-us/dotnet/standard/commandline/get-started-tutorial

Content source URL

https://github.com/dotnet/docs/blob/main/docs/standard/commandline/get-started-tutorial.md

Document Version Independent Id

fa160b1d-66ed-8c5d-37cd-b7e02443e9f1

Platform Id

0f144444-1ce3-cdf7-87a6-c659d44e08d3

Article author

@tdykstra

Metadata

  • ID: 7705dc41-7297-0807-7e9c-2e6548c26b84
  • PlatformId: 0f144444-1ce3-cdf7-87a6-c659d44e08d3
  • Service: dotnet-fundamentals

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions