• Register
1 vote
491 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

Please log in or register to answer this question.

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 2 views
2 views
Problem: 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. How can i solve this?
asked 12 hours ago Ali 4.3k points
0 votes
1 answer 63 views
0 votes
1 answer 30 views
30 views
I have already thrown java.io.IOException, but still, it shows these errors.
asked Dec 28, 2020 TeamScript 13.5k points
0 votes
1 answer 13 views
13 views
Problem: I tried compiling the below but get the following around m16h(x): Line: 16 unreported exception java.lang.Exception; must be caught or declared to be thrown Not sure why though. I've tried various things but it seems I am doing it right.
asked Feb 2 Ali 4.3k points
0 votes
1 answer 1 view
1 view
Problem: 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 ... ae.getSource()==submit) { showfile(); } } public static void main(String ar[]) { new filecontent(); } }
asked 22 hours ago Hira 2.2k points
0 votes
1 answer 1.8K views
1.8K 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 27 views
27 views
I have a Java project I am working on and I get this error.
asked Dec 3, 2020 TeamScript 13.5k points
0 votes
1 answer 10 views
10 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, 2020 Aliza313 720 points
0 votes
1 answer 24 views
24 views
What can cause this exception?
asked Dec 19, 2020 TeamScript 13.5k points