views:

334

answers:

4

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 OutputStreamWriter(
        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 previoud 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!" );
    }
+6  A: 

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
Jon Skeet
+3  A: 

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.

Arne
+1  A: 

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();
    }
}
duffymo
I like your logic. However still I don't know why when I call it, i"m getting the "true"
MAK
@MAK: Because your code never throws an exception.
mikek
It does, throw an Exception...and I see it in the system terminal!
MAK
Could it be Throwable, a non-checked exception? See my revision.
duffymo
+1  A: 

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...

pgras