java - Are there any reasons why some methods in File uses boolean values to indicate its success (instead of just throwing exceptions)? -
the file
class in java contain methods utilize boolean values indicate successfulness of operation being carried out. users of said methods required check return value every time being called.
below snippet of documentations taken mkdir()
stating requirement:
public boolean mkdir()
creates directory named file, assuming parents exist. use mkdirs if want create missing parents.
note method not throw ioexception on failure. callers must check return value.
there's case createnewfile()
(even weirder) use both boolean values thrown exceptions indicate successfulness:
public boolean createnewfile() throws ioexception
creates new, empty file on file system according path information stored in file. this method returns true if creates file, false if file existed. note returns false if file not file (because it's directory, say).
...
note method not throw ioexception if file exists, if it's not regular file. callers should check return value, , may additionally want call isfile.
now, seems inconvenient @ best, because user have anticipate 2 kind of error scenarios instead of using simple try-catch
block.
what's reason behind fuss?
because that's way designed it, on twenty years ago. if can developers out of retirement homes , off zimmer frames might better answer. otherwise guessing.
however don't need call these methods people here seem think. example, isfile()/exists()/delete()/createnewfile()
redundant before new fileinputstream(...)
or new fileoutputstream(...)
, throw exceptions looking for. calling file.exists()/delete()/createnewfile()
before either of these or corresponding filereader/writer
constructors worse useless, positive waste of time , space, doing work constructor (or rather operating system code invoked constructor) has repeat. doubt have ever used file.createnewfile()
in 20 years.
Comments
Post a Comment