-1

I am getting a "unreported exception java.io.ioexception must be caught or declared to be thrown" for some reason. I throw an I/O exception in this method:

    private void setChar() throws IOException
    {
        try
        {
            int data = in.read(); 
            if(data==-1)
            {
                eof = true; 
            }
            else
            {
                currentChar = (char) data; 
            }
        }
        catch (IOException e) 
        {
            System.exit(0);
        }
    }

And I call the method here (in the constructors):

private BufferedReader in;
private char currentChar;
private boolean done;

public Scanner(InputStream inStream)
{
    in = new BufferedReader(new InputStreamReader(inStream));
    done = false;
    getNextChar();
}

public Scanner(String inString)
{
    in = new BufferedReader(new StringReader(inString));
    done = false; 
    setChar();
}

Am I calling / throwing the exception wrong?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Surz
  • 984
  • 3
  • 11
  • 36

3 Answers3

3

Your Scanner constructor can throw an IOException, because it's calling setChar(), and that can throw it.

You must either declare your constructor as throwing the exception, or catch the exception in your constructor and deal with it.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
  • I thought it was fine since I dealt with it in the method? – Surz Aug 29 '14 at 23:23
  • Ah, in that case you need to remove the `throws IOException` from that method, since it can't throw it without catching it and dealing with it. – chiastic-security Aug 29 '14 at 23:24
  • I added the following every tie I used setChar() try{ getNextChar(); } catch(IOException e) { System.exit(0); e.printStackTrace(); } – Surz Aug 29 '14 at 23:24
  • 1
    You only need a `throws` clause for an exception that can fall right through, not for one that you catch. – chiastic-security Aug 29 '14 at 23:24
  • ohhhhhh I see. ok I just took "out' throws from the method declaration and kept the try/catch and it worked. That makes sense- thank you! – Surz Aug 29 '14 at 23:26
1

Your setChar() method says that it can throw an IOException, but your second constructor does not handle it.

You either need to change the setChar() methods signature to not throw the exception (as, in fact, it doesn't throw an IOException), or get you constructor to handle it, for example ...

  public Scanner(String inString)
  {
      in = new BufferedReader(new StringReader(inString));
      done = false;
      try {
          setChar();
      } 
      catch (IOException ie){
          System.exit(1)
      }
  }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DaveH
  • 7,187
  • 5
  • 32
  • 53
1

setChar(); in your constructor throws an IOException.

Therefore, you must catch it in your constructor, or your constructor have to throw an IOException as well.

However, you don't even need to add throws IOException after the declaration of the setChar() method since you are catching potential exceptions inside it.

R2B2
  • 1,541
  • 1
  • 12
  • 19