Java exception handling method

I have a bit of a problem with implementing the following method while handling 3 exceptions that I have to take care of. Should I include try / catch blocks as I do, or what should I leave this for the application instead of class design ?

The method says that I should implement this:

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

This method loads information from the archive specified in the product catalog and returns the catalog.

It starts by opening the file for reading. Then he goes on to read and process each line of the file.

The method is String.startsWithused to determine the type of string:

  • If the string type is Product, the readProduct method is called.
  • If the string type is Coffee, the readCoffee method is called.
  • If the string type is "Brewer", the readCoffeeBrewer method is called.

After processing the line, it loadCatalogadds the product (product, coffee or brewer) to the product catalog.

When all lines of the file have been processed, loadCatalogreturns the product catalog to the method that makes the call.

This method may raise the following exceptions:

  • FileNotFoundException - if the specified files do not exist.
  • IOException - If the error is reading information about the specified file.
  • DataFormatException - if the row has errors (the exception must contain a row with invalid data)

Here is what I still have:

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;
}
+5
source share
3 answers

, . , , . , - . , throws, . , , , catch.

, :

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;
}

( main), loadCatalog, :

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

printStackTrace - .

, loadCatalog , GUI , ( - ).

+1

, , , , , .

, loadCatalog(), , , / , CatalogLoadException loadCatalog() (FileNotFoundException, IOException, DataFormatException), , , .

try {
         ...
    //do this for exceptions you are interested in.
    } catch(Exception e) {
         //maybe do some clean-up here.
         throw new CatalogLoadException(e); // e is the cause.
    }

, loadCatalog() .

, loadCatalog(), : CatalogLoadException.

loadCatalog(String filename) throws CatalogLoadException

, " " . : , , , .

. .


:

, , , throw , catch loadCatalog(), throws , , .

+4

Here is a great article by Heinz Kaboutz regarding exception handling.

http://www.javaspecialists.eu/archive/Issue162.html

0
source

All Articles