• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Check if file Exists

 
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My class makes a remote connection to an iseries machine. I need to check the existance of a file. I use SQL Select to do that. If the file exists I stop my code and tell the user to wait until this file has been processed by accounting. The code needs to proceed if the file does not exist. My code will use SQL to create the file and populate it with data.

When using try and catch if my code detects that a file does not exist it stops processing as expected. How can I make the exception thrown about the none-existant file be okay and finish processing the rest of my code?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds like you are not handling the exception when the file does not exist and it is bubbling up to the caller. You need to Tell The Details so we can see what is going on.
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my class. My question is what to do when shpdsk2 does not exist.

 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you read the link I gave you?

It is also possible to provide too much extra information. For example, if you have a 500 line program, go through it and only post the relevant pieces. Try to narrow your problem down as much as possible.

 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I will try again. What do I do when shpdsk2 does not exist so rest of code will run?

 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens if shpdsk2 does not exist? Do you get an exception from executeQuery()?
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I do. What I need to know is how to avoid this particular exception.
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wrap your execute call in a try-catch block. Catch the exception and set some variable to indicate if the file exists or not.
Is there a more graceful way to figure out if the file exists or not? Is there a table you can query that lists available files (there are such tables-of-tables in Oracle).
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To avoid the exception, don't do things that will cause the exception.

For instance, don't run an SQL Select statement that tries to use the file.
That will cause an exception if it doesn't exist.

Or you could look at it the other way around. If the exception is thrown,
that probably means the file doesn't exist. Use that information.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And this may be a good opportunity to think about moving the SQL code into a DataAccessObject that hides the gory details from the business logic. You might try to change your code so the class you provide can look like this:


The class you provided may very well be the DAO object, but it is smart to split the SQL code from the non-SQL logic when you can, so take the different statements that need to be run, put them in their own methods, and handle any exceptions internal to those methods. Pass the exceptions on in a meaningful way if they can't be dealt with inside the method.

In the case of the shpdsk2 table not existing, it looks like you are handling that as a normal situation, so it would be caught in the method and not passed on (though avoiding it is better as the others have said).
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, going along with Joe's response, something like this should work:

 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This last approach did the trick. Thanks everyone.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic