代码中的异常-Java

代码中的异常-Java,java,exception,try-catch,Java,Exception,Try Catch,我是Java编程新手。我正在准备作业,我需要一些代码方面的帮助。 说明说我需要在numberOfRows()和convertFileArray()方法中处理异常。该练习包括从csv文件中读取、将文件转换为多维数组、在屏幕上打印数组。我的程序运行得很好,唯一的问题是我无法找出在这两种方法中可以处理哪些异常 我写一般例外只是因为我不知道该怎么做。任何建议都会很有帮助。此外,我只是张贴类,我需要把我的尝试捕捉块。 提前谢谢 这是我的密码: import java.io.*; import java.n

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

我写一般例外只是因为我不知道该怎么做。任何建议都会很有帮助。此外,我只是张贴类,我需要把我的尝试捕捉块。 提前谢谢

这是我的密码:

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

 }
import java.io.*;
导入java.nio.file.*;
导入java.util.StringTokenizer;
公共类读取文件{
int行=0;
int列=0;
字符串s=null;
私有字符串[][]数组值;
路径文件路径;
公共读取文件(字符串名称){
filePath=path.get(“C:\\stocks\\”+name);//newMSFT.csv
System.out.println(“输入文件的路径”+filePath.toString());
}
公共布尔值fileExists(){
if(Files.exists(filePath))
返回true;
其他的
返回false;
}
public int numberOfRows(){
试一试{
InputStream数据=新的BufferedInputStream(
newInputStream(filePath));
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(数据));
s=reader.readLine();
while(s!=null){
行++;
s=reader.readLine();
}    
}
捕获(例外e){
System.out.println(“消息:+e”);
}
返回行;
}
public void convertFileArray(){
试一试{
InputStream数据=新的BufferedInputStream(
newInputStream(filePath));
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(数据));
s=reader.readLine();
StringTokenizer z=新的StringTokenizer,“,”;
columns=z.countTokens();
arrayValues=新字符串[行][列];
对于(int x=0;x>”;
对于(int a=0;a
您正在做的大部分事情看起来都是抛出的东西。

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

在您的程序中,在
convertFileArray()
numberOfRows()
中有一个
IOException
,您的方法会抛出
IOException

try {
      s = reader.readLine();
    } catch (IOException e) {
      e.printStackTrace();
    }
并抛出以下异常:

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

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

numberOfRows
method中有FileNotFoundException和IOException查看Java API中调用的方法(尤其是Java.io等中的方法-io方法喜欢抛出异常)并查看它们抛出的异常类型。捕捉每一个错误,思考在这种情况下可以做些什么,并向用户报告错误或根据需要进行修复。