Java异常处理方法(Java exception handling method)

系统教程 行业动态 更新时间:2024-06-14 16:58:30
Java异常处理方法(Java exception handling method)

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

该方法说我应该实现这一点:

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

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

它通过打开文件开始阅读。 然后继续读取并处理文件的每一行。

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

如果行的类型是“Product”,则调用readProduct方法。 如果行的类型是“Coffee”,则调用readCoffee方法。 如果行的类型是“Brewer”,则调用readCoffeeBrewer方法。

生产线处理完成后, loadCatalog将产品(产品,咖啡或啤酒)添加到产品目录中。

当文件的所有行都被处理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; }

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

最满意答案

总的想法是,你将例外渗透到适当的地方来处理它们。 我猜你的导师希望他们能够主要处理。 在这种情况下,我可以猜测,因为你给出了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; }

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

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

用适当的东西替换printStackTrace。

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

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).

更多推荐

本文发布于:2023-04-15 03:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/08a373fa466c42c2966568f9c03abf17.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   方法   Java   method   handling

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!