开发者

Why Java return statment inside the catch block not working?

开发者 https://www.devze.com 2022-12-20 21:52 出处:网络
Why does the following code always return true even when an exception is thrown? public boolean write (ArrayList<String> inputText, String locationToSave){

Why does the following code always return true even when an exception is thrown?

public boolean write (ArrayList<String> inputText, String locationToSave){

    try {           
        File fileDir = new File(locationToSave);
        Writer out = new BufferedWriter(new OutputStre开发者_高级运维amWriter(
        new FileOutputStream(fileDir), "utf8"));

        int index = 0;
        int size = inputText.size();
        while (index < size) {
                    out.append(inputText.get(index));
                    out.append("\n");
                    index++;
                    }
        out.flush();
        out.close();

        return true;

   } catch (UnsupportedEncodingException e) {
        System.out.println("UnsupportedEncodingException is : \n" + e.getMessage());
        return false;
   } catch (IOException e) {
        System.out.println("IOException is : \n" + e.getMessage());
        return false;
   } catch (Exception e) {
        System.out.println("Exception is : \n" + e.getMessage());
        return false;
   }
}

Edition 01

This is the code I'm using to test the previous code:

 if (fileReader.write(fileReader.read(selectedFile), selectedSaveLocation)) {
        System.out.println("The file : " + selectedFile + " as been successfully"
        + "converted to : " + selectedSaveLocation );
    } else {
        System.out.println("The file : " + selectedFile + " failed to convert!" );
    }


I don't think you're seeing what you think you're seeing. In other words, I'm pretty sure it's actually returning false, and that you should check the calling code.

For example, I pasted your code into a new Java console app, made it static, and wrote a main method with this body:

System.out.println(write(null, null)); 

The output was:

Exception is : 
null
false


It does not always return true. I've created a testproject, caused an IOException ... and get false! There must be an error in your reasoning.


If you're seeing an exception in the console, and the return value is still true, then check the type of exception. Since you catch Exception, I'd guess that it could be a non-checked Throwable that's being triggered. You wouldn't ever set the flag to false in that case.

I might write it this way:

public boolean write (Collection<String> inputText, String locationToSave)
{

    boolean isSuccessful = false;
    Writer out;

    try
    {

        File fileDir = new File(locationToSave);
        out = new BufferedWriter(new OutputStreamWriter(
        new FileOutputStream(fileDir), "utf8"));

        for (String line : inputText)
        {
            out.append(inputText.get(index));
            out.append("\n");
        }

        isSuccessful = true;
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        cleanup(out);
    }    

    return isSuccessful;
}

private static void cleanup(Writer out)
{
    try
    {
        if (out != null)
        {
            out.flush();
            out.close();
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}


As everyone already said, the exception is not the one you think it is. I would guess the method

fileReader.read(selectedFile)

logs the exception you see in your logs...

Show us the code of this method... And also show us the exception...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号