[java] Error message "unreported exception java.io.IOException; must be caught or declared to be thrown"

Error:

filecontent.java:15: unreported exception java.io.IOException; must be caught or declared to be thrown

showfile(); ^ filecontent.java:78: unreported exception java.io.IOException; must be caught or declared to be thrown

showfile(); ^

I have already thrown java.io.IOException, but still it shows these errors.

My code:

import java.awt.*;
import java.awt.event.*;
import java.io.*;

class filecontent extends Frame implements ActionListener
{
    TextField t[] = new TextField[4];
    TextArea ta[] = new TextArea[4];
    Button submit;
    Panel p1;
    filecontent()
    {
        setGUI();
        setRegister();
        showfile();
        setTitle("FileData");
        setVisible(true);
        setSize(300, 300);
        /*  addWindowListener(new WindowAdapter()
            { 
                public void windowClosing(WindowEvent we)
                { 
                    System.exit(0); 
                }
            }); 
        */

    }

    void setGUI()
    {
        p1 = new Panel();
        p1.setLayout(new GridLayout(5, 4, 10, 10));
        for(int i=0; i<4; i++)
        {
            t[i] = new TextField(10);
            ta[i] = new TextArea("");
            p1.add(t[i]);
            p1.add(ta[i]);
        }
        submit = new Button("Submit");
        p1.add(submit);
    }

    void setRegister()
    {
        submit.addActionListener(this);
    }

    void showfile() throws java.io.IOException
    {
        FileReader fin[] = new FileReader[4];
        FileReader fn;
        //   br[]=new BufferedReader[4];

        for(int i=0;i<4;i++)
        {
            fin[i]=new FileReader(t[i].getText());
        }
        int cnt = 1;
        String s;
        fn = fin[0];
        BufferedReader br = new BufferedReader(fn);
        while(cnt <= 4)
        {
            if((s=br.readLine()) != null)
            {
                ta[cnt-1].append(s+"");
            }
            else
            {
                cnt++;
                fn = fin[cnt-1];
                ta[cnt-1].setText("");
            }
        }
    }

    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==submit)
        {
            showfile();
        }
    }

    public static void main(String ar[])
    {
        new filecontent();
    }
}

This question is related to java ioexception throws

The answer is


Exceptions bubble up the stack. If a caller calls a method that throws a checked exception, like IOException, it must also either catch the exception, or itself throw it.

In the case of the first block:

filecontent()
{
    setGUI();
    setRegister();
    showfile();
    setTitle("FileData");
    setVisible(true);
    setSize(300, 300);

    /*
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        });
    */
}

You would have to include a try catch block:

filecontent()
{
    setGUI();
    setRegister();
    try {
        showfile();
    }
    catch (IOException e) {
        // Do something here
    }
    setTitle("FileData");
    setVisible(true);
    setSize(300, 300);

    /*
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        });
    */
}

In the case of the second:

public void actionPerformed(ActionEvent ae)
{
    if (ae.getSource() == submit)
    {
        showfile();
    }
}

You cannot throw IOException from this method as its signature is determined by the interface, so you must catch the exception within:

public void actionPerformed(ActionEvent ae)
{
    if(ae.getSource()==submit)
    {
        try {
            showfile();
        }
        catch (IOException e) {
            // Do something here
        }
    }
}

Remember, the showFile() method is throwing the exception; that's what the "throws" keyword indicates that the method may throw that exception. If the showFile() method is throwing, then whatever code calls that method must catch, or themselves throw the exception explicitly by including the same throws IOException addition to the method signature, if it's permitted.

If the method is overriding a method signature defined in an interface or superclass that does not also declare that the method may throw that exception, you cannot declare it to throw an exception.


When the callee throws an exception i.e. void showfile() throws java.io.IOException the caller should handle it or throw it again.

And also learn naming conventions. A class name should start with a capital letter.


void showfile() throws java.io.IOException  <-----

Your showfile() method throws IOException, so whenever you use it you have to either catch that exception or again thorw it. Something like:

try {
  showfile();
}
catch(IOException e) {
  e.printStackTrace();
}

You should learn about exceptions in Java.


The error message means that any method that calls showfile() must either declare that it, in turn, throws IOException, or the call must be inside a try block that catches IOException. When you call showfile(), you do neither of these; for example, your filecontent constructor neither declares IOException nor contains a try block.

The intent is that some method, somewhere, should contain a try block, and catch and handle this exception. The compiler is trying to force you to handle the exception somewhere.

By the way, this code is (sorry to be so blunt) horrible. You don't close any of the files you open, the BufferedReader always points to the first file, even though you seem to be trying to make it point to another, the loops contain off-by-one errors that will cause various exceptions, etc. When you do get this to compile, it will not work as you expect. I think you need to slow down a little.


Your method showFile() declares that it can throw an IOException. Since this is a checked exception, any call to showFile() method must handle the exception somehow. One option is to wrap the call to showFile() in a try-catch block.

 try {
     showFile();
 }
 catch(IOException e) {
    // Code to handle an IOException here
 }