C# Exception

exception when there is error occurred. To safeguard the exception during run time, use try catch for operations that may cause error.

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

System.Exception class contains some useful information about the error.
System.Exception ex;
string ex.Message; //descriptive text of the error
System.Collection.IDictionary ex.Data;
string ex.Source; //the name of class or object that caused error



endmemo.com © 2024  | Terms of Use | Privacy | Home