You declared rawData
inside the try
block.
The variable does not exist outside of it.
In particular, what would happen if the try
block exits before that line?
You need to move the declaration outside the try
.
سؤال
Why do I get the error: rawData cannot be resoled in the finally block?
in:
public void parseData(String fileName) throws IOException
{
try
{
DataInputStream rawData = new DataInputStream(new FileInputStream(fileName));
/* here I'm gonna do something*/
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
finally
{
rawData.close();
}
}
Thank you
المحلول
You declared rawData
inside the try
block.
The variable does not exist outside of it.
In particular, what would happen if the try
block exits before that line?
You need to move the declaration outside the try
.
نصائح أخرى
rawData
is not visible to the finally
block because it was declared in the try
block.
public void parseData(String fileName) throws IOException
{
DataInputStream rawData = null;
try
{
rawData = new DataInputStream(new FileInputStream(fileName));
/* here I'm gonna do something */
} catch (FileNotFoundException e)
{
e.printStackTrace();
} finally
{
if (rawData != null)
{
rawData.close();
}
}
}
u should declare rawData outside try/catch/finaly block
public void parseData(String fileName) throws IOException
{
DataInputStream rawData;
try
{
rawData = new DataInputStream(new FileInputStream(fileName));
/* here I'm gonna do something*/
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
finally
{
rawData.close();
}
}