[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 300
  • Last Modified:

java exception during file operation

try
{
FileWriter fstream = new FileWriter(filename);
BufferedWriter out = new BufferedWriter(fstream);      

so some processing on out.
out.close();                                                         
}

catch (Exception ex)
{
out.close();
}                     

===
I want to open a file and do some processing.
I want to close the file once everything is done.

If there is any exception
I am ccatching them but not able to close the file.
so when I keep the above code in catch, it gives cannot find symbol.

so when I keep the "out" decalartion outside try
 unreported exception java.io.IOException; must be caught or declared to be thrown
   FileWriter fstream = new FileWriter(filename);

can you correct me?
0
vkchaitu82
Asked:
vkchaitu82
  • 4
  • 3
  • 2
1 Solution
 
for_yanCommented:
Why you close it twice?
0
 
objectsCommented:
BufferedWriter out = null;
try
{
FileWriter fstream = new FileWriter(filename);
out = new BufferedWriter(fstream);      

so some processing on out.
out.close();                                                        
}

catch (Exception ex)
{
if (out!=null) out.close();
}                    
0
 
for_yanCommented:

"when I keep the above code in catch, it gives cannot find symbol" - strange - please post the bigger piece of code
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
objectsCommented:
or use a finally block


BufferedWriter out = null;
try
{
FileWriter fstream = new FileWriter(filename);
out = new BufferedWriter(fstream);      

so some processing on out.
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
if (out!=null) out.close();
}                    
0
 
vkchaitu82Author Commented:
Objects, the code you gave is not working as it gives
 unreported exception java.io.IOException; must be caught or declared to be thrown
0
 
for_yanCommented:

Is this about this RSS ffeds program?
Works for me already  for several days without any problems.

Keep them all in one try catch
loop, and don't catch IOException,
catch just Exception and you'll have no problems
with any of that.



0
 
vkchaitu82Author Commented:
nope this is for general file operations.

I have only one exception I dont catchIOExceptions.

the flow is same as what objects: mentioned.
0
 
objectsCommented:
>  unreported exception java.io.IOException; must be caught or declared to be thrown

you also need to handle any exceptions from close()

BufferedWriter out = null;
try
{
FileWriter fstream = new FileWriter(filename);
out = new BufferedWriter(fstream);      

so some processing on out.
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
try {
    if (out!=null) out.close();
} catch (IOException ex) {
   ex.printStackTrace();
}
}                    
0
 
for_yanCommented:
Well, if you want to be rigoristic, use finally.
All my life I open and close files and live without finally quite well
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now