Results 1 to 6 of 6
  1. #1
    DoolinDalton is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default what exception to throw

    Hopefully you will see the gist of my question from the following pseudo java code.

    I have a method that looks like this. It is supposed to make an ftp connection.

    public static FTPClient ftpConnect (param list... ) throws SocketException, IOException, Exception {
    FTPClient f = new FTPClient();
    f.connect(...); // this can throw SocketException and IOException
    if (!f.login(...)) {throw Exception();} // <- Is this bad?
    return f;
    }

    The part that I have a question on (which I commented above) is what I am doing when the login() method returns a false. I want the calling program to "die" when the login is not successful, so I want to throw an exception here. But I'm not sure what to throw. So I'm throwing the master of all exceptions, which is Exception.

    Is this a bad practice? My method signature does list the other exceptions that can come from my method.

  2. #2
    DoolinDalton is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    Well, let me extend on what I wrote above. If my method throws SocketException, IOException, Exception then the calling program cannot specifically catch for the event that login() returned false because it is throwing Exception which includes the other two exceptions.

    So I see this probably is not a good things to do, so going back to my original question, how do I know what to throw if login() returns false?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm no pro at this, but how about throwing a new FalseLoginException(...) -- an exception class that you've created specifically for this problem?
    Last edited by Fubarable; 02-09-2010 at 11:32 PM.

  4. #4
    DoolinDalton is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    I was hoping not to create a new exception class because
    (1) Im new to JAVA and don't know how to create one
    (2) I have a bunch of other methods that are like this, where I'm using something that returns false and I want my method to "die" when a false is returned... which would mean I need to create a specific exception class for each one of these thingies that can return a false.

    But if creating a new exception class for something like this is typical, then I would appreciate knowing that.... or how would I find something that "fits" my scenario from the myriad of exception classes?

    And lastly, if I were to find something, would I have to make sure it is not a parent of the other classes being thrown from my method because if it were then, I run into the same problem of not being able to segregate the exceptions.... am I making sense?
    Last edited by DoolinDalton; 02-09-2010 at 11:57 PM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default

    I almost always create a set of new exceptions for some functionality.
    Usually with a parent exception that they then extend, just to try and keep things neat.

    It's pretty standard.

    An exception is easy to write:
    Java Code:
    public class FailedToLoginException extends Exception {
    }
    If you want to add some constructors for passing in messages you can, but if all you want to know is that some particular exception occurred then the above is perfectly valid.

  6. #6
    DoolinDalton is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 05-28-2009, 06:09 PM
  2. throw exception
    By GIRISH PATEL in forum New To Java
    Replies: 4
    Last Post: 04-23-2009, 05:35 AM
  3. throw an exception
    By sfe23 in forum New To Java
    Replies: 3
    Last Post: 02-14-2009, 05:41 AM
  4. Replies: 1
    Last Post: 12-13-2008, 02:24 AM
  5. Difference between Throws and Throw
    By Poonam in forum New To Java
    Replies: 7
    Last Post: 02-06-2008, 05:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •