C# trycatch

try{...}catch(){...}finally{...} is used for catching exceptions, and preventing program interruption.

string file = "text.txt";
System.IO.FileInfo fileInf = new System.IO.FileInfo(file);
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// Opens a file stream to read the file
System.IO.FileStream fs = fileInf.OpenRead();
try
{
contentLen = fs.Read(buff, 0, buffLength);
// Till Stream content ends
while (contentLen != 0)
{
System.Console.WriteLine(buff);
contentLen = fs.Read(buff, 0, buffLength);
}
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.Message);
}
finally
{
fs.Close();
}


endmemo.com © 2017  Terms of Use | Home