Results 1 to 6 of 6
Thread: what exception to throw
- 02-09-2010, 09:46 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 4
- Rep Power
- 0
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.
- 02-09-2010, 10:24 PM #2
Member
- Join Date
- Feb 2010
- Posts
- 4
- Rep Power
- 0
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?
-
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 10:32 PM.
- 02-09-2010, 10:41 PM #4
Member
- Join Date
- Feb 2010
- Posts
- 4
- Rep Power
- 0
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 10:57 PM.
- 02-10-2010, 09:51 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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:
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.Java Code:public class FailedToLoginException extends Exception { }
- 02-10-2010, 03:45 PM #6
Member
- Join Date
- Feb 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
whether this will throw 'InterruptedException' or 'IllegalStateException'
By vysh in forum New To JavaReplies: 2Last Post: 05-28-2009, 05:09 PM -
throw exception
By GIRISH PATEL in forum New To JavaReplies: 4Last Post: 04-23-2009, 04:35 AM -
throw an exception
By sfe23 in forum New To JavaReplies: 3Last Post: 02-14-2009, 04:41 AM -
Link an external compiler to the eclipse environment throw the console in eclispe
By adolf111 in forum EclipseReplies: 1Last Post: 12-13-2008, 01:24 AM -
Difference between Throws and Throw
By Poonam in forum New To JavaReplies: 7Last Post: 02-06-2008, 04:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks