【问题标题】:Exceptions in my code - Java我的代码中的异常 - Java
【发布时间】:2013-03-27 04:20:10
【问题描述】:

我是 Java 编程新手。我正在准备一项作业,我需要一些关于我的代码的帮助。 说明说我需要在numberOfRows()convertFileArray() 方法中处理异常。该练习包括读取 csv 文件、将文件转换为多维数组、在屏幕上打印数组。我的程序运行良好,唯一的问题是我无法弄清楚我可以在这两种方法中处理哪些异常。

我写了一般例外只是因为我不知道该怎么做。任何建议都会非常有帮助。此外,我只发布需要放置 try-catch 块的课程。 提前致谢。

这是我的代码:

import java.io.*;
import java.nio.file.*;
import java.util.StringTokenizer;

public class ReadFiles {
    int rows = 0;
    int columns = 0;
    String s = null;
    private String[][] arrayValues;
    Path filePath;

    public ReadFiles(String name) {

        filePath = Paths.get("C:\\stocks\\" + name);      //newMSFT.csv
        System.out.println("Path for file entered " + filePath.toString());

    }

    public boolean fileExists() {

        if(Files.exists(filePath))
            return true;

        else 
            return false;
    }

    public int numberOfRows() { 

        try {
            InputStream data = new BufferedInputStream(
              Files.newInputStream(filePath));
            BufferedReader reader = new BufferedReader(
              new InputStreamReader(data));
            s = reader.readLine();

            while(s != null) {
                rows++;
                s = reader.readLine();
            }    
        }

        catch(Exception e) {
            System.out.println("Message: " + e);
        }

        return rows;
    }

    public void convertFileArray() {

        try {
            InputStream data = new BufferedInputStream(
              Files.newInputStream(filePath));
            BufferedReader reader = new BufferedReader(
              new InputStreamReader(data));

            s = reader.readLine();   

            StringTokenizer z = new StringTokenizer(s, ",");
            columns = z.countTokens();

            arrayValues = new String[rows][columns];

            for(int x = 0; x < rows; x++) {
                z = new StringTokenizer(s, ",");

                //when there are still more tokens, place it in the array:
                int y = 0;
                while(z.hasMoreTokens()) {
                    arrayValues[x][y] = z.nextToken();
                    y++;
                }

                s = reader.readLine();
            }

            System.out.println("An array was created and has " +
              rows + " rows and " + columns + " columns.");
        } 

        catch(Exception e) {
            System.out.println("Message: " + e);
            e.printStackTrace();
        }
    }

    public void printArray() {  

        System.out.println("The data from the array is >> ");

        for(int a = 0; a < rows; a++) {
            String output = null;    

            for(int b = 0; b < columns; b++) {
                System.out.print(arrayValues[a][b] + "  ");

            }
            System.out.println();
        }
    }

    public String[][] getArrayValues() {
        return arrayValues;
    }

 }

【问题讨论】:

  • numberOfRows方法中有FileNotFoundException和IOException
  • 查看您调用的方法的 Java API(尤其是 java.io 等中的方法 - IO 方法喜欢抛出异常),看看它们抛出了哪些类型的异常。抓住他们每个人,想想在这种情况下可以做什么,并向用户报告错误或适当地修复它。

标签: java exception try-catch


【解决方案1】:

您的readLine 方法抛出IOException

try {
      s = reader.readLine();
    } catch (IOException e) {
      e.printStackTrace();
    }

Files.newInputStream(filePath) 抛出以下异常:

  • IllegalArgumentException - 如果指定了无效的选项组合
  • UnsupportedOperationException - 如果指定了不受支持的选项
  • IOException - 如果发生 I/O 错误
  • SecurityException - 在默认提供程序的情况下,并且安装了安全管理器,将调用 checkRead 方法来检查对文件的读取访问权限。

因此,您要如何处理以及处理哪个异常取决于您。

【讨论】:

    【解决方案2】:

    您所做的大部分事情看起来都是抛出IOException

    【讨论】:

      【解决方案3】:

      如果您使用像 eclipse 这样的 IDE,那么它会告诉您需要处理哪些异常。

      在您的程序中,convertFileArray()numberOfRows() 中有一个 IOException

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多