简体   繁体   English

尝试在返回方法中捕获块

[英]try and catch blocks in return methods

I'm fairly new to using try and catch statements and I'm having a bit of trouble with return statements. 我对使用try和catch语句相当陌生,并且在return语句方面遇到了一些麻烦。 Upon compiling my code I got an unreported IOException in: 编译我的代码后,我在以下地方得到了一个unreported IOException
fileFtp = client.listNames(); So I tried wrapping this statement in a try and then wrote a catch for the exception but then the fileName array was no longer visible, so I wrapped the entire method in a try{} and used a catch at the end and put the return statement which returned the fileName after the end of the catch{} , but this didn't work either since fileName was no longer visible as it was in the try{} So how can I meet the exception report requirements while still having fileName and fileFtp arrays initialized? 因此,我尝试尝试将此语句包装起来,然后为异常编写了一个catch,但是fileName数组不再可见,因此我将整个方法包装在try{}并在最后使用了catch并放入return语句在catch{}结束后返回了fileName,但由于在try{}不再可见fileName,因此这也不起作用,因此如何在仍然具有fileName和fileFtp数组的情况下满足异常报告的要求初始化了吗?

 private String[] checkDirectories(){

    String[] fileFtp;
    String[] fileName;

    try{        
        fileFtp = client.listNames();
    }
    catch(IOException ex){
        System.out.println("IOException in checkDirectories()");
        ex.printStackTrace();     
    }

    fileName = new String[fileFtp.length];
        for (int i=0; i<fileFtp.length; i++){
            fileName[i] =   fileFtp[i];
        }            
        return fileName;
 }

You shouldn't be catching the exception here, when you do that you don't give the calling code any indication that the operation failed. 您不应该在这里捕获异常,否则您不会给调用代码任何表明操作失败的指示。 Let the exception be thrown so that the caller can realize that something went wrong and deal with it: 让异常抛出,以便调用者可以意识到出了点问题并加以处理:

private String[] checkDirectories() throws IOException {

    String[] fileFtp = client.listNames();
    String[] fileName = new String[fileFtp.length];
    for (int i=0; i<fileFtp.length; i++) {
        fileName[i] =   fileFtp[i];
    }
    return fileName;
}

Throwing an exception lets the code get out of its current path of execution so that it's not operating on invalid data, and it signals to calling code that something went wrong. 引发异常会使代码脱离当前的执行路径,从而使其无法对无效数据进行操作,并向调用代码发出信号,指出发生了问题。 If you catch the exception and return null here than calling code has to check for null and decide whether or not to proceed. 如果捕获到异常并在此处返回null,则调用代码必须检查null并决定是否继续。

I am not sure what you asked but you should probably try this: 我不确定您要问什么,但您应该尝试以下操作:

private String[] checkDirectories()
{

    String[] fileFtp = null;
    String[] fileName = null;
    try
    {
    fileFtp = client.listNames();
     fileName = new String[fileFtp.length];
        for (int i=0; i<fileFtp.length; i++)
        {
            fileName[i] =   fileFtp[i];
        }
    }
        catch(IOException ex)
{
    System.out.println("IOException in checkDirectories()");
    ex.printStackTrace();

}



        return fileName;
}

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

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