Note that there are some explanatory texts on larger screens.

plurals
  1. POJava exception handling method
    primarykey
    data
    text
    <p>I'm having a little bit of trouble implementing the following method while handling the 3 exceptions I'm supposed to take care of. <strong>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</strong>?</p> <p>The method says I'm supposed to implement this:</p> <pre><code>public Catalog loadCatalog(String filename) throws FileNotFoundException, IOException, DataFormatException </code></pre> <p>This method loads the info from the archive specified in a catalog of products and returns the catalog. </p> <p>It starts by opening the file for reading. Then it proceeds to read and process each line of the file.</p> <p>The method <code>String.startsWith</code> is used to determine the type of line:</p> <ul> <li>If the type of line is "Product", the method readProduct is called.</li> <li>If the type of line is "Coffee", the method readCoffee is called.</li> <li>If the type of line is "Brewer", the method readCoffeeBrewer is called.</li> </ul> <p>After the line is processed, <code>loadCatalog</code> adds the product (product, coffee or brewer) to the catalog of products. </p> <p>When all the lines of the file have been processed, <code>loadCatalog</code> returns the Catalog of products to the method that makes the call. </p> <p>This method can throw the following exceptions:</p> <ul> <li><code>FileNotFoundException</code> — if the files specified does not exist. </li> <li><code>IOException</code> — If there is an error reading the info of the specified file. </li> <li><code>DataFormatException</code> — if a line has errors(the exception must include the line that has the wrong data)</li> </ul> <p>Here is what I have so far:</p> <pre><code>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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload