Database Reference
In-Depth Information
catch (DirectoryNotFoundException ex)
{
MessageBox.Show(ex.Message + " " + "Please provide valid Directory
name", "File Read Error");
}
In any case, whether the file is read or not, a Stream object needs to be closed. Performing such
mandatory operations can be the best candidate for a finally block. Also, as you will notice in the case
of an exception, the Stream object will not be initialized because the file name or path will not be found
and so can't be closed.
Hence, you have to check whether the Stream object you created is null before you close it.
finally
{
if (sr != null)
{
sr.Close();
}
}
Summary
In this chapter, you learned about exception handling and also how to handle exceptions thrown by a C#
file I/O program. In the next chapters dedicated to ADO.NET, you will apply the exception-handling
principles throughout the ADO .NET code.
Specifically, in the next chapter, you'll look at creating an ADO.NET connection to a SQL Server 2012
database.
 
Search WWH ::




Custom Search