简体   繁体   English

在包含 try-catch 块的方法中返回一个对象 (Java)

[英]Returning an object within a method containing try-catch block (Java)

I wanted to return an object within a method that has a try-catch block.我想在具有 try-catch 块的方法中返回一个对象。 As shown below the method takes in a CSV file, reads the serialNumber and is supposed to return an object of the book with attributes such as name, year, etc., from the CSB file.如下所示,该方法接受一个 CSV 文件,读取serialNumber并应该从 CSB 文件中返回具有名称、年份等属性的书籍对象。 I want to just return this object but I don't know where in the scope to return it given that foundName is local and thus I won't be able to return the new object r .我只想返回这个对象,但我不知道在范围内返回它的位置,因为foundName是本地的,因此我将无法返回新对象r

public static ReadCSVExample1 readBook(String filename, String serialNumber){
        try{
            String line = "";
            BufferedReader br = new BufferedReader(new FileReader(filename));
            String foundName = "";
            while ((line = br.readLine()) != null){
                String[] values = line.split(",");
                for (int z = 0; z<values.length; z++){
                    if (values[z].equals(serialNumber)){
                        System.out.println(values[z]+ " found!");
                        foundName = values[z+1];
                        System.out.println(values[z+2]);
                        System.out.println(values[z+3]);
                    }
                }
            }
        } catch (FileNotFoundException e){
            System.err.println(e);
        } catch (IOException e){
            System.err.println(e);
        }ReadCSVExample1 r = new ReadCSVExample1(foundName);
        return r;
    }

Just move the foundName outside the try-catch block like so:只需将foundName移动到try-catch块之外,如下所示:

public static ReadCSVExample1 readBook(String filename, String serialNumber){
       String foundName = "";
        try{
            String line = "";
            BufferedReader br = new BufferedReader(new FileReader(filename));
            
            while ((line = br.readLine()) != null){
                String[] values = line.split(",");
                for (int z = 0; z<values.length; z++){
                    if (values[z].equals(serialNumber)){
                        System.out.println(values[z]+ " found!");
                        foundName = values[z+1];
                        System.out.println(values[z+2]);
                        System.out.println(values[z+3]);
                    }
                }
            }
        } catch (FileNotFoundException e){
            System.err.println(e);
            return null;
        } catch (IOException e){
            System.err.println(e);
            return null;
        }ReadCSVExample1 r = new ReadCSVExample1(foundName);
        return r;
    }

If it's not outside the try-catch block, the scope of foundName is within the try-catch block.如果它不在 try-catch 块之外,则foundName的范围在 try-catch 块内。 If you move it outside, it's scope is the entire method.如果你把它移到外面,它的范围就是整个方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM