• Post Reply Bookmark Topic Watch Topic
  • New Topic

Best Practice on determining valid file path

 
Bd Howard
Greenhorn
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

What is the best practice on determining if a user has entered a valid file name and/or path to a file?

Is it best to test it with the File class exist() method, or should I just let it go and throw a FileNotFoundException?

In the code below, if a user enters a path into a JTextField, filename is not null or empty. If the user leaves that blank, there is the default Assignment05.txt that is used as the input.

What is the proper way to deal with that exception and return to the GUI? I don't want the program to crash, merely return after handling the exception and showing a dialog so they can try again.



Cheers,

BD
 
Bd Howard
Greenhorn
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it best to handle the exception where it happens, or is it better to pass it up the chain to my GUI? The code I pasted above is from the class where the real work happens, and that class is called from a try/catch in my GUI, so is it best to handle it in the GUI then, since I am trying to get updated user input?

Is writing a while loop in the catch block good practice? I picture the loop running until they enter a valid file path. If I test their new path, and it throws a FileNotFoundException, how do I handle that, since I am already at the top of the exception handling tree?

BD
 
Bd Howard
Greenhorn
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've handled it by using the while loop and file.exists() in the catch block. It works, but I still don't know if this is the best method. :-(

BD
 
Paul Clapham
Sheriff
Posts: 21884
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bd Howard wrote:What is the best practice on determining if a user has entered a valid file name and/or path to a file?


A file which you propose to read from? Then the best practice is to start reading from it, and if you get a FileNotFoundException then you know it isn't "valid".

By the way I don't see the code which asks the user for a file name, but the best way to do that is to use a JFileChooser. This allows the user to choose an existing file, rather than forcing them to type the name of the file correctly.
 
Bd Howard
Greenhorn
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never considered using the JFileChooser. Thanks for that tip.

Where do you handle the FNFE? Do you do it at the point it is thrown, or pass it up the tree? I guess I'm looking for a rule of thumb, since I am sure there are instances where you would have to handle it locally and others where you'd pass it up the tree. Do you have a preference, and then code to that preference?

The files in question are to be read from, no writes.

Cheers

BD
 
Paul Clapham
Sheriff
Posts: 21884
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bd Howard wrote:I am sure there are instances where you would have to handle it locally and others where you'd pass it up the tree.


Yes, this is correct. You always have to make this choice when you're catching an exception. And as you suggest, the answer always depends on the design of the application.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Bd Howard wrote:I am sure there are instances where you would have to handle it locally and others where you'd pass it up the tree.


Yes, this is correct. You always have to make this choice when you're catching an exception. And as you suggest, the answer always depends on the design of the application.


The general rule though is "throw early, catch late." Most of the time, the code where the exceptions occur isn't in a position to decide what to do about, so it just informs the caller, either by not catching the exception in the first place, or by catching, wrapping in something more "layer-appropriate", and throwing that.

Sometimes it may be able to retry (for instance if it's code that you know should have no reason to fail the vast majority of the time, but may occasionally due to timing issues) or provide some default value (if the value it was supposed to produce was optimal but not necessary, and getting some value, even a sub-optimal one, is better than failing). In my experience, however, those cases are rare.
 
Bd Howard
Greenhorn
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both, for the responses. The more I learn about Java, the more I am daunted by just how much more I have to learn. :-) I took a quick look at the Servlet forum and thought I was reading Greek. :-)

So it would seem in my specific case that I should throw the FNFE back up to my GUI and handle it there. I did a quick and dirty loop to get a valid file where the exception was thrown, but I think I'll change that so I can get some practice on throwing. And I want to implement Paul's suggestion about the JFileChooser.

Is a program ever really done? :-)

BD
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!