You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Jeff Duska <Je...@noaa.gov> on 2003/07/23 00:12:09 UTC

[Net] FTPClient and finally questions...

I am very confused. I'm moving my code base from the enterprisedt
library to use the Jakarta Commons Net library. I have it downloading
files like a charm. Today, I was writing up a few simple jUnit tests to
make sure the exceptions for no files and connection timeout worked.
I've been having problems all day. Here is a section of code from my
getFiles() method.

    _files = ftpClient.listNames(mask);

            if (_files == null || _files.length ==0) {
                log.warn("No files to download for mask " + mask);
                throw new FTPServerException("No files found!");
            }

        // code to download the files

        } catch (IOException e) {
            String msg = "FTP Server Error Occurred" +
                         e.getMessage();
            log.warn(msg);
            throw new FTPServerException(msg);
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException f) {
                    // do nothing
                }
            }
            return _files;
        }


I throw a FTPServerException when listNames returns a zero length array
or null. The problem is my FTPServerException is getting lost.  I can
follow the exception into the finally cause. Once I return from this
method, java forgets about my exception that I threw. In my example unit
test, shown below, I don't get a FTPServerException thus my test fails.

  public void testFileDownloadFailure() throws FTPServerTimeoutException
{
        try {
            // setup will creates an instance of FTPServer
            // FTP server should cause a FTPServerException
            _ftp.getFiles("myfile", ".");
            fail();
        } catch (FTPServerException  e) {
            // do nothing, we expect this to happen
        }
    }

It seems like the finally cause is somehow "eating" my exception, yet
there is no other exception being raised. Does anyone see why this is
happening?

I copied the example pretty much out of the JavaDocs and the example
ftp.java file. I looked at the InformIT article. This example doesn't
bother to disconnect if there is a problem. I would think that this
would cause the system to leak network resources,  or will the the
garbage collection free up these resources. If this isn't an issue, I
could just remove the finally cause. Actually, I'm going to do this as
work-around, but I'd still like to know why finally cause isn't working.

Thanks for the help,

Jeff Duska





Re: [Net] FTPClient and finally questions...

Posted by Jeff Duska <Je...@noaa.gov>.
Jeff:

Thanks for the example. It turns out the problem was with my placement of the return statement. I had mine inside the
finally statement. I guess this is troublesome.  I was told on my blog that if you return from a finally, any exception that
had been thrown is discarded.  It suppose to be explained in
http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#236653, but sure I didn't get that.

Thanks,

Jeff Duska




Re: [Net] FTPClient and finally questions...

Posted by "Jeffrey D. Brekke" <jb...@brekke.org>.
Sorry about the previous mail, it didn't have my explanation present.
The test attached is passing here and was quickly created from your
code snippets.  Maybe you have some other exception being thrown after
the FTPServerException?  You test is erroring, not failing right?

Anyway the test case is working for me and I didn't attach the exception class since it is trivial.

Good luck, 

jb
-- 
=====================================================================
Jeffrey D. Brekke                                   jbrekke@wi.rr.com
Wisconsin,  USA                                     brekke@apache.org
                                                    ekkerbj@yahoo.com


[Net] Why would I get a NullPointerException in getLocalAddress?

Posted by Jeff Duska <Je...@noaa.gov>.
I tweak the Jeffrey D. Brekke's trycatchfinallytest example. I'm now getting NullPointerException  in getLocalAddress line
481 of SocketClient.java. Sometimes it work perfectly, then it just stop working. Any ideas of what might be causing this?

Regards,

Jeff Duska