[jcifs] SmbException Supercedes boolean?
James Nord
teilo at teilo.net
Tue Oct 30 10:30:18 EST 2001
Rob Wygand wrote:
> James,
>
> The argument for exceptions being thrown was made a long time ago. I'm
> still not sure which is the 100% right approach, but there needed be
> be some mechanism whereby the reason the operation failed could be
> determined. If I cannot create a new file because of a network
> failure, a permissions issue, or the disk is full, I might need to do
> that, and just a boolean return value doesn't do it. Exceptions do.
> I've talked before about having some other mechanism (such as an some
> sort of error handler class), but Mike seemed set on Exceptions.
Sorry I was not clear enough, I was meaning to use both..
For example
isDirectory()
if the SMBFile is a plain file -> false
if the SMBFile is a Directory -> true
If the file does not exists/no access etc... -> exception
I personally don't want to have to be forced write a hell of a lot of
ugly error handling code if *my* app doesn't care.
Sure - if app XYZ wants that info it should be able to get it easily as
well ;-)
of course for creating a files etc.. this gets more tricky - you would
need to use multiple methods or change them to something like
boolean mkdir(boolean throwException) throws Exception
or
boolean mkdir()
boolean mkdirEX() throws Exception
e.g. // find out if a smbfile is a dir and then create a new dir
with only exceptions
psudeocode ;-)
SMBFile f1 = new SMBFile(someSMBURL);
SMBFIle f2=new SMBFile(f, documentname);
try {
if (f1.isDirectory()) {
try {
if (f2.makeFile()) {
// do stuff
}
else {
handleUnabletoCreateFile()
}
}
catch (Exception e) {
// cannot create file... handle this
handleUnabletoCreateFile()
}
}
else {
// whoops not a directory.... assuning there was no error we
still get here
// code handle for non created dir...
handleNotDir()
}
}
catch (SMBFileException) {
handlenotDir()
}
OKay we can optimise this but instincvly with non exceptions
SMBFile f1 = new SMBFile(someSMBURL);
SMBFIle f2=new SMBFile(f, documentname);
if (f1.isDirectory()) {
if (f2.makeFile() {
// do stuff
}
else {
handleUnabletoCreateFile()
}
}
else {
handlenotDir();
}
I know which one I would rather read ;-)
/James
> rjw
>
> James Nord wrote:
>
>> Well I would go the other way and return boolean...
>>
>> Better design as exceptions should not be used to test if an
>> opperation was succesfull...
>> Makes for cleaner code/programming structure (if used correctly)
>> (IMHO which goes 100% against the other reply ;-) )
>>
>> Also - its what the java libraries do...
>>
>> mkdir()
>> public abstract boolean mkdir()
>> Creates a directory, the pathname of which is specified by this
>> XFileAccessor object.
>> Returns true if the directory could be created; false otherwise.
>>
>>
>> Just my religion...
>>
>> Besides, does it harm to return boolean? It may come a time where its
>> benifical to the code.
>> Is it not possible to not make a directory/file etc.. and not throw a
>> exception?
>> What about isDirectory() ???
>> That *must* return boolean and possibly throw an exception?
>>
>> /James
>>
>> Rob Wygand wrote:
>>
>>> Mike,
>>>
>>> I recently encountered this, and I think it's best to return void.
>>> We had some functions that returned boolean, but always threw
>>> exceptions on errors, and what we found was that some people caught
>>> the exceptions and did nothing with them, only using the return
>>> value of the function. This, obviously, caused confusion and errors.
>>> It's just a much clearer contract when void is returned.
>>>
>>> rjw
>>>
>>> Allen, Michael B (RSCH) wrote:
>>>
>>>> Does anyone have an opinion as to whether or not SmbFile methods
>>>> should
>>>> return boolean now that many throw SmbException? For example, 0.5.1
>>>> has:
>>>>
>>>> public boolean mkdir()
>>>>
>>>> which looks like it's java.io.File counterpart and returns true if
>>>> the directory was
>>>> successfully created and false otherwise. For 0.6.0 I was planning
>>>> on just
>>>> adding the throws to this like:
>>>>
>>>> public boolean mkdir() throws SmbException
>>>>
>>>> but returning boolean is rather pointless given that any kind of
>>>> failure provokes
>>>> an SmbException. I don't know of any other context that might make
>>>> use of
>>>> boolean. I think this should just be:
>>>>
>>>> public void mkdir() throws SmbException
>>>>
>>>> Any opinions? Obviously this is pretty important because it's
>>>> undoubtledly
>>>> going to break code so I'd rather only do this once.
>>>>
>>>> Mike
>>>>
>>>>
>>>
>>>
>>
>>
>>
>
More information about the jcifs
mailing list