Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Everytime I run this code I get
PrimeNumberList.java:31: error: unreported exception IOException; must be caught or declared to be thrown
                export(result);

and I cant figure out why.
Java
<pre lang="java">


import javax.swing.JOptionPane;
import java.io.*;

public class PrimeNumberList
{
	public static void main(String[] args)
	{
		String start, end, filename;
		String result = "";
		String numlist = "";
		int numstart, numend;
		
		JOptionPane.showMessageDialog(null, "This program will take any 2 numbers " + 
			"you give and return a file with every prime number between those numbers");
		
		start = JOptionPane.showInputDialog("Enter a starting number");
		numstart = Integer.parseInt(start);
		
		end = JOptionPane.showInputDialog("Enter a ending number");
		numend = Integer.parseInt(end);
		
		
		
		for (int i = numstart; i <= numend; i++)
		{
			if (isPrime(i))
			numlist = numlist + i + " ";
		}
		result = " a list of primes from " + numstart +
			" to " + numend + " is: " + numlist;
		export(result);
		
		System.exit(0);
	}
	
	
	
	public static boolean isPrime(int num)
	{
		boolean divisorFound = false;
		int count = 2;
		
		while(count < num && !divisorFound)
		{
			if((num % count) == 0)
				divisorFound = true;
			count ++;
		}
		
		return !divisorFound;
	}
	
	public static void export(String result) throws IOException
   {
      // Open the file.
      PrintWriter outputFile = new PrintWriter("PrimeNumberList.txt");


         // Write the name to the file.
      outputFile.println(result);
	  
	  outputFile.close();
    }
}


What I have tried:

I have changed the export variable changed the export methods string name and tried setting the string name equal to a new variable before writing it to the file
Posted
Updated 24-Jun-20 12:04pm
Comments
ZurdoDev 24-Jun-20 11:41am    
Which line of code throws the error?
Chase Rowe 24-Jun-20 11:42am    
line 31
ZurdoDev 24-Jun-20 11:44am    
And which line might that be?
Chase Rowe 24-Jun-20 11:45am    
export(result);

In Java, you can catch checked exceptions (such as the one you are facing right now) in a try...catch block as suggested in Solution 1:
Java
// try...catch only needs to be around the error-prone code.
try {
   export(result); 
} catch (Exception error) {
   // error handling logic
}
Or you can add a throws modifier to the function and let it bubble up. Like this:
Java
public static void main(String[] args) throws IOException { // <-- add this
   // main code here
}
Check out these threads to learn more on this:
java - Throwing exception in main method - Stack Overflow[^]

Of course, you can always use a try...catch to handle it automatically/manually in your code. The reason why we need to use either one of these is that in Java ignoring checked exceptions are treated as errors on compile time—which is good for several reasons, especially for the beginners who are not aware of Java Security, Java IO, and other practices or gotchas. There are some ways to work around with this to "disable" the checked exceptions, but I highly discourage that practice; but if you want to see, check Is disabling Checked Exceptions in Java possible? - Stack Overflow[^]
 
Share this answer
 
From a quick google search this means that you have to catch the exception. See Error message "unreported exception java.io.IOException; must be caught or declared to be thrown" - Stack Overflow[^].

Your export method says it will throw an IO exception but when you call export it is not inside of a try catch that will catch that exception.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900