The requested page is not available for the requested platform. You are viewing the content for Default platform.

Try statement without catch or finally

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;
}