• Register
1 vote
231 views

Problem :

I am very new to Java. I am trying to execute my Java code but facing below error.

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

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

showfile(); ^

I have already thrown the java.io.IOException, however still it shows me above errors.

6 5 3
7,540 points

2 Answers

0 votes

Solution :

This error message means that the method that calls your showfile() must either declare  it, or use throws IOException, or your call should be inside the try block that catches the IOException. When you try to call showfile(), you are probably doing neither of these ; So for example, the filecontent constructor is neither declaring the IOException nor it contains the try block.

The motto is that your some method, somewhere, must contain the try block,  and handle the current exception. Your compiler is trying to force you to handle your exception some where in your code.

You don't seem to close any of your files you open, your BufferedReader always points to your first file, even though it seems to be trying to make it point to the another but the loops contain off-by-one errors that will cause various exceptions in your code execution, etc.

9 7 4
38,600 points
0 votes

Solution:

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

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.

readLine() declares that it throws an IOException. This is a checked exception, meaning you can't just ignore it. You need to either catch it, e.g.:

try {
    a = Double.parseDouble ( kb.readLine () ) ;
} catch (IOException e) {
    System.out.println("Can't read a"); // Or something more intellegent
}

Or, if you don't have any intelligent way to handle it, you could just throw it upwards by adding this exception to the caller's signature, e.g.:

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

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 970 views
970 views
Problem : I want to create a simple program that will output a string to a text file. Below is my code : import java.io.*; public class JavaTesting { public static void main(String[] args) { File myfile = new File ("myfile.txt"); myfile.getParentFile().mkdirs(); ... for process is 1. As I am very new to Java, I am unable to understand the error. Can anybody help me in resolving the error?
asked Nov 25, 2019 alecxe 7.5k points
0 votes
1 answer 9 views
9 views
The process cannot access the file 'file path' because it is being used by another process, What does this mean, and what can I do about it?
asked Aug 28 Aliza313 720 points
0 votes
0 answers 14 views