Skip to main content

Try statement without catch or finally

In This Article

CodeRush Classic shows the Try statement without catch or finally code issue if a try statement does not contain catch and finally blocks.

#Fix

Add a catch or finally block to the try statement.

#Purpose

Highlights the try statements, which would cause the Expected catch or finally compilation error.

#Example

public string LoadText(string fileName)
{try
    {
        return File.OpenText(fileName).ReadToEnd();
    }
    return null;
}

Fix:

public string LoadText(string fileName)
{
    try
    {
        return File.OpenText(fileName).ReadToEnd();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    return null;
}