分享

unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown

 gearss 2016-12-27

I am creating a class -- just a class, no main() and I am receiving the error of "unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown" at this line:

FileOutputStream outStr = new FileOutputStream(FILE, true);   

I don't understand; I put in a try{} catch{} block and it's still reporting the error.

Additionally, it's also reporting an "illegal start of type" for the try and both catch lines, and it's also saying that ';' is expected for both catch lines.

I'm using the NetBean IDE, FYI.

Thank you for any help.

Here is the full code:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.FileNotFoundException;


public class UseLoggingOutputStream 
{

    String FILE = "c:\\system.txt";

    try
    {

        FileOutputStream outStr = new FileOutputStream(FILE, true);

    }

    catch(FileNotFoundException fnfe)
    {

        System.out.println(fnfe.getMessage());

    }

    catch(IOException ioe)
    {

        System.out.println(ioe.getMessage());

    }

}
shareimprove this question
2 
What is this line: Logger.getLogger("outLog"), Level.ERROR)));? fix it first. – MByD Apr 21 '11 at 19:18
1 
You don't need to put so many //Begin //End comments in the code at the most obvious places. They are creating more noise than being informative. – kunal Apr 21 '11 at 19:18 
   
Fixed. I had thought I deleted that line for the purposes of this question/answer post but I ostensibly missed it. And I removed the //Begin and //End comments. Thank you. – user717236 Apr 21 '11 at 19:31

You need to put the file processing statements inside a method:

import java.io.FileOutputStream;
import java.io.FileNotFoundException;

public class UseLoggingOutputStream {
    public void myMethod() {
        String file = "c:\\system.txt";
        try {
            FileOutputStream outStr = new FileOutputStream(file, true);
        } catch(FileNotFoundException fnfe) { 
            System.out.println(fnfe.getMessage());
        } 
    }
}
shareimprove this answer

All functional code needs to go into methods - I don't see a method in your code - that's the illegal start of type problem. The other compile errors should become clearer once you get the basics down.

public class Foo {

  public void doSomething() {
    //code here 
  }

}
shareimprove this answer
1 
All your code goes in --> public static void main(String[] args) { – Ishmael Apr 21 '11 at 19:19 
   
Yea, that fixed the issue, I didn't have a method, I suppose. As far as the public static void main, I was trying to create a library class. Doesn't a library class not need a main method? – user717236 Apr 21 '11 at 19:26
2 
In Java the method is the operational unit. Except for a static initializer block in a class or class level variables, all code is in methods. You main, with the signature Ishmael provice, if this is a class Java will launch directly, i.e. java Foo means that Foo must have a main. Library classes with helper methods or modeling entities do not need a main. – karakuricoder Apr 21 '11 at 22:47

Move this code to some method or at least to a static initializer block.

shareimprove this answer
import java.io.*;
import java.util.*; 

public class SortNames {

private String[] strings = new String[10];  

private int counter;

public SortNames() {

ArrayList<String> names = new ArrayList<String>();
Scanner scan = null;
File f = null;

try{
 f = new File("names.txt");
 scan = new Scanner(f);

  while(scan.hasNext()) names.add(scan.next());
}
  finally{scan.close();}

Collections.sort(names);

  for(String s:names) System.out.println(s);

     }  
} 
shareimprove this answer

Sorry if this isn't helpful to you, but I was able to solve this exact issue by adding " throws FileNotFoundException " to my method call that contained the FileWriter. I know this may not be helpful since you aren't using methods, but then again, maybe it is.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约