Java异常处理方法

edu*_*222 5 java exception-handling

在处理我应该处理的3个异常时,我在实现以下方法时遇到了一些麻烦.我应该像我正在做的那样包含try/catch块,还是将它留给应用程序而不是类设计

该方法说我应该实现这个:

public Catalog loadCatalog(String filename)
         throws FileNotFoundException, IOException, DataFormatException
Run Code Online (Sandbox Code Playgroud)

此方法从产品目录中指定的存档加载信息并返回目录.

首先打开文件进行阅读.然后继续阅读并处理文件的每一行.

该方法String.startsWith用于确定行的类型:

  • 如果行的类型是"Product",则调用readProduct方法.
  • 如果行的类型是"Coffee",则调用readCoffee方法.
  • 如果行的类型是"Brewer",则调用readCoffeeBrewer方法.

生产线后,loadCatalog将产品(产品,咖啡或酿酒商)添加到产品目录中.

处理完文件的所有行后,loadCatalog将产品目录返回给进行调用的方法.

此方法可以抛出以下异常:

  • FileNotFoundException - 如果指定的文件不存在.
  • IOException - 如果读取指定文件的信息时出错.
  • DataFormatException - 如果一行有错误(例外必须包含错误数据的行)

这是我到目前为止:

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;
}
Run Code Online (Sandbox Code Playgroud)

Tof*_*eer 1

总体思路是,将异常渗透到适当的位置来处理它们。我猜你的老师希望它们在 main 中处理。在这种情况下,我可以猜测是因为给了你 throws 子句。一个简单的经验法则是,如果方法在 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;
}
Run Code Online (Sandbox Code Playgroud)

然后在调用 loadCatalog 的方法(大概是 main)中,您将拥有:

try
{
   loadCatalog(...);
}
catch(FileNotFoundException ex)
{
    ex.printStackTrace(); 
}
catch(IOException ex)
{
    ex.printStackTrace(); 
}
catch(DataFormatException ex)
{
    ex.printStackTrace(); 
}
Run Code Online (Sandbox Code Playgroud)

用适当的东西替换 printStackTrace 。

这样,loadCatalog 方法就不会处理显示错误消息,因此您可以在 GUI 或控制台代码中调用该方法,并且调用它的代码可以选择如何向用户显示错误(或在某种方式)。