0

I'm writing a program that uses IOExceptions and I'm coming across an error that I can't figure out. The code is

Main:

public class IOJ {
  public static void main(String[] args) {
     findStuff calc = new findStuff();


    calc.findSyl();
              //^ Unreported exception IOException, must be caught or declared to be thrown
    calc.printRes();
  }
}

and the actual files that's throwing

import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;

public class findStuff {
  private double findSyl; //find Syl
  private double printRes; //print results

  public double NumSylG = 0; //number of syllables

    public double findSyl()throws IOException {

        BufferedReader inputStream = null;
        PrintWriter outputStream = null;

        try {
            String newLine = "";
            outputStream.println(newLine);
            String[] tokens = newLine.split(" ");

            char temp;

            for (int i = 0; i < newLine.length(); i++) {
              temp = newLine.charAt( i );

              if (Character.isLetter(temp) )
                 NumSylG++;
            }

        } catch (IOException e) {
             System.err.println("Caught IOException: " + e.getMessage());
        } finally {
            if (inputStream != null)
                inputStream.close();
            if (outputStream != null)
                outputStream.close();
        }

        return findSyl;
    }

    public double printRes() {
    System.out.printf("The total number of Syl in Gettysburg is: %.1f \n", NumSylG);

    return printRes;
    }
}

The findStuff file compiles just fine. it's when I call it from main is when I get that error. I'm still getting used to catching and throwing things so can anyone give me some insight on what I am doing wrong? Note: I didn't put the file path for privacy reasons.

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
  • possible duplicate of [Why do I get "Exception; must be caught or declared to be thrown" when I try to compile my Java code?](http://stackoverflow.com/questions/908672/why-do-i-get-exception-must-be-caught-or-declared-to-be-thrown-when-i-try-to) – Tom Nov 16 '14 at 18:52
  • Class names should be `UpperCase` in Java. – Erik Kaplun Nov 16 '14 at 20:06

1 Answers1

0
public double findSyl()throws IOException

findSyl() declares that it throws an IOException, which is a checked exception. This means that the caller of findSyl() must either catch that exception or declare that it may throw that exception too.

Your two options :

1.

  public static void main(String[] args)
  {
     findStuff calc = new findStuff();
     try {
         calc.findSyl();
         calc.printRes();
     }
     catch (IOException ex) {
         // handle the exception here
     }  
  }

2.

  public static void main(String[] args) throws IOException
  {
     findStuff calc = new findStuff();   
     calc.findSyl();
     calc.printRes();   
  }
Eran
  • 387,369
  • 54
  • 702
  • 768