try-catch-finally (Referenční dokumentace jazyka C#)
Běžné použití a společně je získání a použití prostředků v bloku, řešení výjimečných okolností v bloku a uvolnění prostředků catch finally v try catch finally bloku.
Další informace a příklady opětovného vyvolání výjimek najdete v tématu try-catch a Throwing Exceptions. Další informace o bloku finally najdete v tématu try-finally.
Příklad
public class EHClass
{
void ReadFile(int index)
{
// To run this code, substitute a valid path from your local machine
string path = @"c:\users\public\test.txt";
System.IO.StreamReader file = new System.IO.StreamReader(path);
char[] buffer = new char[10];
try
{
file.ReadBlock(buffer, index, buffer.Length);
}
catch (System.IO.IOException e)
{
Console.WriteLine("Error reading from {0}. Message = {1}", path, e.Message);
}
finally
{
if (file != null)
{
file.Close();
}
}
// Do something with buffer...
}
}
specifikace jazyka C#
Další informace najdete v části s příkazem try specifikace jazyka C#.