-1

Why am I getting a "must be caught or declared to be thrown" error with this code ? All I want is to test a bunch of code by pasting it into a new java program what is the easiest way to bunch of code ?

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(new File("C:\\Users\\User\\Selenium\\scrapjv\\interface\\NASDAQlist.txt"));
        List<String> symbolList = new ArrayList<String>();
        while (sc.hasNextLine()) {
            symbolList.add(sc.nextLine());
        }

        PrintWriter logput = new PrintWriter("C:\\Users\\User\\Selenium\\scrapjv\\interface\\log.txt", "UTF-8");
        for (String symb : symbolList) {
            System.out.println(symb);
        }
        logput.close();
    }
}
user2864740
  • 60,010
  • 15
  • 145
  • 220
Wicelo
  • 2,358
  • 2
  • 28
  • 44
  • [Search for error messages](http://stackoverflow.com/search?q=%22must+be+caught+or+declared+to+be+thrown%22) *before* asking. "Easiest" way is to add `throws Exception` to the main method definition; find a relevant [near-]duplicate for details. (Sadly almost every duplicate is itself a poor question with an equally poor title.) – user2864740 Aug 31 '14 at 22:55
  • 1
    Ahh http://stackoverflow.com/questions/11589302/why-is-throws-exception-necessary-when-calling-a-function seems better than most and http://stackoverflow.com/questions/4392446/when-to-use-throws-in-a-java-method-declaration?rq=1 – user2864740 Aug 31 '14 at 23:00

2 Answers2

2

Some of the methods you're calling can throw FileNotFoundException if the file isn't found:

 public Scanner(File source) throws FileNotFoundException
 public PrintWriter(String fileName) throws FileNotFoundException

Java's compiler checks that some thrown exceptions -- those other than RuntimeException and its subclasses -- are either caught or declared thrown. Compilation will fail otherwise. This helps find some errors at compile-time, before the program is ever run.

One option is to declare your calling function to throw the exception or a superclass:

 public static void main(String[] args) throws FileNotFoundException {

A better option in this case is to catch the exception and do something with it. For example, here's how you can do that for the Scanner() exception:

    File inFile = new File("C:\\Users\\User\\Selenium\\scrapjv\\interface\\NASDAQlist.txt");
    try {
        Scanner sc = new Scanner( inFile );
        List<String> symbolList = new ArrayList<String>();
        while (sc.hasNextLine()) {
            symbolList.add(sc.nextLine());
        }
    }
    catch ( FileNotFoundException e ) {
        System.out.println("Could not find file: " + inFile.getAbsolutePath());
    }
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
1

Your two Scanner declaration lines have a chance to throw an exception, which are basically errors that happen after the code is executed (because of this they are sometimes called runtime errors). Because the compiler knows that your code might make a FileNotFoundException happen, it requires you to catch the exception.

This is done by enclosing the code in a try-catch block.

try {

    Scanner sc = new Scanner(new File("C:\\Users\\User\\Selenium\\scrapjv\\interface\\NASDAQlist.txt"));
    List<String> symbolList = new ArrayList<String>();
    while (sc.hasNextLine()) {
        symbolList.add(sc.nextLine());
    }


    PrintWriter logput = new PrintWriter("C:\\Users\\User\\Selenium\\scrapjv\\interface\\log.txt", "UTF-8");
    for (String symb : symbolList) {
        System.out.println(symb);
    }
    logput.close();


} catch (java.io.FileNotFoundException ex)
{
    ex.printStackTrace();
}
BitNinja
  • 1,477
  • 1
  • 19
  • 25