Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 911879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:17:08+00:00 2026-05-15T17:17:08+00:00

I’m having a little bit of trouble implementing the following method while handling the

  • 0

I’m having a little bit of trouble implementing the following method while handling the 3 exceptions I’m supposed to take care of. Should I include the try/catch blocks like I’m doing or is that to be left for the application instead of the class design?

The method says I’m supposed to implement this:

public Catalog loadCatalog(String filename)
         throws FileNotFoundException, IOException, DataFormatException

This method loads the info from the archive specified in a catalog of products and returns the catalog.

It starts by opening the file for reading. Then it proceeds to read and process each line of the file.

The method String.startsWith is used to determine the type of line:

  • If the type of line is “Product”, the method readProduct is called.
  • If the type of line is “Coffee”, the method readCoffee is called.
  • If the type of line is “Brewer”, the method readCoffeeBrewer is called.

After the line is processed, loadCatalog adds the product (product, coffee or brewer) to the catalog of products.

When all the lines of the file have been processed, loadCatalog returns the Catalog of products to the method that makes the call.

This method can throw the following exceptions:

  • FileNotFoundException — if the files specified does not exist.
  • IOException — If there is an error reading the info of the specified file.
  • DataFormatException — if a line has errors(the exception must include the line that has the wrong data)

Here is what I have so far:

public Catalog loadCatalog(String filename)
       throws FileNotFoundException, IOException, DataFormatException{
    String line = "";
    try {
        BufferedReader stdIn = new BufferedReader(new FileReader("catalog.dat"));
            try {
                BufferedReader input = new BufferedReader(
                    new FileReader(stdIn.readLine()));
                while(! stdIn.ready()){
                    line = input.readLine();                        
                    if(line.startsWith("Product")){
                        try {
                            readProduct(line);
                        } catch(DataFormatException d){
                            d.getMessage();
                        }
                    } else if(line.startsWith("Coffee")){
                        try {
                            readCoffee(line);                               
                        } catch(DataFormatException d){
                            d.getMessage();
                        }
                    }  else if(line.startsWith("Brewer")){
                        try {
                            readCoffeeBrewer(line);
                        } catch(DataFormatException d){
                            d.getMessage();
                        }
                    }
                }
            } catch (IOException io){
                io.getMessage();
            }
    }catch (FileNotFoundException f) {
        System.out.println(f.getMessage());
    }
    return null;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:17:09+00:00Added an answer on May 15, 2026 at 5:17 pm

    The general idea is that you percolate exceptions up to the appropriate place to handle them. I am going to guess that your instructor expects them to be handled in main. In this case I can guess that because of the throws clause you were given. A simple rule of thumb is that if the method declares the exception in the throws clause you do not catch it in that method. So the method you are writing should have no catch statements.

    To do that you would change your code something like:

    public Catalog loadCatalog(String filename) 
        throws FileNotFoundException, 
               IOException, 
               DataFormatException
    {
        String line = "";
    
        BufferedReader stdIn = new BufferedReader(new FileReader("catalog.dat"));
        BufferedReader input = new BufferedReader(new FileReader(stdIn.readLine()));
    
        while(!stdIn.ready())
        {
            line = input.readLine();
    
            if(line.startsWith("Product"))
            {
                readProduct(line);
            } 
            else if(line.startsWith("Coffee"))
            {
                readCoffee(line);
            }  
            else if(line.startsWith("Brewer"))
            {
                readCoffeeBrewer(line);
            }
        }
    
        return null;
    }
    

    and then in the method (presumably main) that calls loadCatalog you would have:

    try
    {
       loadCatalog(...);
    }
    catch(FileNotFoundException ex)
    {
        ex.printStackTrace(); 
    }
    catch(IOException ex)
    {
        ex.printStackTrace(); 
    }
    catch(DataFormatException ex)
    {
        ex.printStackTrace(); 
    }
    

    replacing the printStackTrace with something appropriate.

    That way the method, loadCatalog, doesn’t deal with displaying the error messages, so you can call the method in GUI or console code and the code that calls it can choose how to display the error to the user (or deal with it in some way).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.