11 Replies - 663 Views - Last Post: 21 October 2015 - 07:35 AM Rate Topic: -----

#1 lasalla39130454   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-October 15

Keep getting "must be caught or declared to be thrown"

Posted 21 October 2015 - 05:32 AM

I Keep getting "must be caught or declared to be thrown" for all 9 errors

import java.util.Scanner;
import java.io.*;
// TASK #3 Add the file I/O import statement here

/**
   This class reads numbers from a file, calculates the
   mean and standard deviation, and writes the results
   to a file.
*/

public class StatsDemo
{
   // TASK #3 Add the throws clause
   public static void main(String[] args)
   {
      double sum = 0;      // The sum of the numbers
      int count = 0;       // The number of numbers added
      double mean = 0;     // The average of the numbers
      double stdDev = 0;   // The standard deviation
      String line;         // To hold a line from the file
      double difference;   // The value and mean difference

      // Create an object of type Scanner
      Scanner keyboard = new Scanner (System.in);
      String filename;     // The user input file name

      // Prompt the user and read in the file name
      System.out.println("This program calculates " +
                         "statistics on a file " +
                         "containing a series of numbers");
      System.out.print("Enter the file name:  ");
      filename = keyboard.nextLine();

      // ADD LINES FOR TASK #4 HERE
      // Create a FileReader object passing it the filename
          FileReader freader = new FileReader (filename);
      // Create a BufferedReader object passing FileReader
          BufferedReader  breader = new BufferedReader (freader);
      // Perform a priming read to read the first line of
      // the file
          line = breader.readLine();
      // Loop until you are at the end of the file
          while (line != null)
      {
        // Convert the line to a double value and add the
        // value to sum
            sum += Double.parseDouble(line);
        // Increment the counter
            count ++;
        // Read a new line from the file
            line = breader.readLine();
      }
      // Close the input file
          breader.close();
      // Store the calculated mean
          mean = sum/count;

      // ADD LINES FOR TASK #5 HERE
      // Reconnect FileReader object passing it the
      // filename
          freader = new FileReader(filename); 
      // Reconnect BufferedReader object passing
      // FileReader object
          breader = new BufferedReader(freader);
      // Reinitialize the sum of the numbers
          sum = 0;
          count = 0;
      // Perform a priming read to read the first line of
      // the file
          line = breader.readLine();
      // Loop until you are at the end of the file
          while (line != null)
      {
        
      // Convert the line into a double value and
      // subtract the mean
            difference = Double.parseDouble(line) - mean;
      // Add the square of the difference to the sum
            sum += Math.pow(difference,2);
      // Increment the counter
              count++;
      // Read a new line from the file
            line = breader.readLine();
      }
      // Close the input file
          breader.close();
      // Store the calculated standard deviation
          stdDev = Math.sqrt(sum/count);

      // ADD LINES FOR TASK #3 HERE
      // Create a FileWriter object using "Results.txt"
          FileWriter fwriter=new FileWriter("Results.txt");
      // Create a PrintWriter object passing the
      // FileWriter object
          PrintWriter pwriter = new PrintWriter (fwriter);
      // Print the results to the output file
          pwriter.printf("mean = %.3f\r\n",mean);
          pwriter.printf("stdDev = %.3f\r\n",stdDev);
      // Close the output file
          pwriter.close();
   }
}

This post has been edited by andrewsw: 21 October 2015 - 05:36 AM
Reason for edit:: added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Keep getting "must be caught or declared to be thrown"

#2 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6778
  • View blog
  • Posts: 27,973
  • Joined: 12-December 12

Re: Keep getting "must be caught or declared to be thrown"

Posted 21 October 2015 - 05:36 AM

Square brackets are not code tags:

Attached Image
Was This Post Helpful? 0
  • +
  • -

#3 lasalla39130454   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-October 15

Re: Keep getting "must be caught or declared to be thrown"

Posted 21 October 2015 - 05:39 AM

View Postandrewsw, on 21 October 2015 - 05:36 AM, said:

Square brackets are not code tags:

Attachment codetags.gif



thank you, but was that the error that caused it to not compile?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6778
  • View blog
  • Posts: 27,973
  • Joined: 12-December 12

Re: Keep getting "must be caught or declared to be thrown"

Posted 21 October 2015 - 05:42 AM

Note also that I have changed your topic title, "im having trouble figuring out exactly what the error is" tells us nothing about your particular problem.

View Postlasalla39130454, on 21 October 2015 - 12:39 PM, said:

View Postandrewsw, on 21 October 2015 - 05:36 AM, said:

Square brackets are not code tags:


thank you, but was that the error that caused it to not compile?

No, nothing to do with it. It appears that you didn't watch the screencast.
Was This Post Helpful? 0
  • +
  • -

#5 lasalla39130454   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-October 15

Re: Keep getting "must be caught or declared to be thrown"

Posted 21 October 2015 - 05:44 AM

Thank you again, this is my first post, but im not sure if it is fixed?

This post has been edited by andrewsw: 21 October 2015 - 05:45 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6778
  • View blog
  • Posts: 27,973
  • Joined: 12-December 12

Re: Keep getting "must be caught or declared to be thrown"

Posted 21 October 2015 - 05:47 AM

Look at your first post. Your code is now in a code block, with line numbers. So, yes, I added the code tags for you.
Was This Post Helpful? 0
  • +
  • -

#7 lasalla39130454   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-October 15

Re: Keep getting "must be caught or declared to be thrown"

Posted 21 October 2015 - 05:51 AM

im talking about the error in the code thats causing it to not compile
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6778
  • View blog
  • Posts: 27,973
  • Joined: 12-December 12

Re: Keep getting "must be caught or declared to be thrown"

Posted 21 October 2015 - 05:56 AM

Post the exact error details.
Was This Post Helpful? 0
  • +
  • -

#9 lasalla39130454   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-October 15

Re: Keep getting "must be caught or declared to be thrown"

Posted 21 October 2015 - 05:59 AM

its telling me on lines 36 41 51 54 61 70 83 86 92 that i have an unreported exception and it must be caught or declared to be thrown
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6778
  • View blog
  • Posts: 27,973
  • Joined: 12-December 12

Re: Keep getting "must be caught or declared to be thrown"

Posted 21 October 2015 - 07:12 AM

Post the full, exact error details. In particular, these probably tell you what exceptions you are expected to report.

The Catch or Specify Requirement

Quote

Valid Java programming language code must honor the Catch or Specify Requirement. This means that code that might throw certain exceptions must be enclosed by either of the following:

  • A try statement that catches the exception. The try must provide a handler for the exception, as described in Catching and Handling Exceptions.
  • A method that specifies that it can throw the exception. The method must provide a throws clause that lists the exception, as described in Specifying the Exceptions Thrown by a Method.

Code that fails to honor the Catch or Specify Requirement will not compile.

This post has been edited by andrewsw: 21 October 2015 - 07:12 AM

Was This Post Helpful? 1
  • +
  • -

#11 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 976
  • View blog
  • Posts: 3,849
  • Joined: 13-June 14

Re: Keep getting "must be caught or declared to be thrown"

Posted 21 October 2015 - 07:13 AM

The problem is that those lines in question throw what are called checked exceptions, which means, that as the error says, you either need to handle them (i.e. with a try-catch) or put a "throws" statement at the end of the method signature listing the exceptions that can be thrown. I think the first is preferable really - you should try and deal with problems when they occur.

You can look at the Java API documentation for the methods you're using to find out which exceptions they throw.
Was This Post Helpful? 1
  • +
  • -

#12 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11474
  • View blog
  • Posts: 19,538
  • Joined: 19-March 11

Re: Keep getting "must be caught or declared to be thrown"

Posted 21 October 2015 - 07:35 AM

Let's look at the first line you cite:

Quote

FileReader freader = new FileReader (filename);


Consulting the API documentationfor this, we see

Quote

public FileReader(String fileName)
throws FileNotFoundException
Creates a new FileReader, given the name of the file to read from.
Parameters:
fileName - the name of the file to read from
Throws:
FileNotFoundException - if the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.



What this means is that calling this method might result in a FileNotFoundException being thrown. Since this is a checked exception, the compiler requires that you handle it: rather than just hoping everything goes well, Java enforces a certain discipline. (other languages, for the most part, do not do this, but Java does)

The way to handle it is to wrap the dangerous code in a try block, and use the catch block to deal with the interesting question of "what happens if the file can't be opened?"

Your code will look something like this:

FileReader freader = null;  // declare it in the outer scope

try {
    freader = new FileReader (filename);
}
catch (FileNotFoundException fne) {  
  // do something about it here. Make sure you prevent attempts to access the null freader. 
}


It's up to you to fill in the catch part.


Two important things to keep in mind about exception handling. First, you want to not get in the habit of catching "Exception e". catch blocks should be specific, because they're meant to handle specific problems. "Exception e" masks problems, which is a major bug in your code. The other is, you should make your try blocks as small as possible. Identify the "danger zone" and wrap just that area. It'll usually be just a line or two. A common mistake among beginning Java programmers is to try to wrap an entire method in a try. This makes exception handling very difficult, and should be avoided.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1