Hi there, i am trying to use Error Handling to wait for a File Not Found error
try
{
TextReader tr = new StreamReader(Path);
}
catch(FileNotFoundException fnf)
{
StatusLabel.Text = "File Not Found !";
}
If i do this and try and run the program, it can not see the TextReader tr instance further down in my code... Any ideas ?
Thanks in advance
Event Handling
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.

AlanRank #669 · 3.6K reputationPosted Sep 19, 2008, 1:57 AM EDT
If you only want it to be visible within the containing method, then define the TextReader variable outside the try statement:
TextReader tr = null;
try
{
tr = new StreamReader(Path);
}
catch(FileNotFoundException fnf)
{
StatusLabel.Text = "File Not Found !";
}
if (tr != null)
{
// do something with text reader
}
If you want it to be visible in other methods, then you'll need to define it at class level.