2

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());

    }

}
user717236
  • 4,959
  • 19
  • 66
  • 102
  • 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. – kdabir 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

5 Answers5

9

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());
        } 
    }
}
climmunk
  • 1,141
  • 1
  • 12
  • 29
  • I get this when doing the same thing inside main – Ungeheuer Mar 28 '17 at 02:42
  • @Ungeheuer You still need to wrap the FileOutputStream construction in a try/catch block in main. Alternately you can declare a method to throw a FileNotFoundException. – climmunk Mar 29 '17 at 02:43
  • I did have it in try/catch. Ended up needing to use stdin anyways, so just switched to that. – Ungeheuer Mar 29 '17 at 03:26
  • Glad you found a solution! If you want to understand what your original problem was, consider posting a question with your code. – climmunk Mar 30 '17 at 05:44
4

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 
  }

}
karakuricoder
  • 1,065
  • 8
  • 8
  • 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
1

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

kdabir
  • 9,623
  • 3
  • 43
  • 45
1
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);

     }  
} 
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
ekta
  • 23
  • 6
0

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.