• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

if statement inside try/catch block

 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is the following code problematic?
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sam,
Welcome to Java Ranch!
Why is it problematic? Well, there are a couple of things going on here that I'm not too happy about, plus the issue you're actually asking about. Let's add some line numbers, and then take a look:

Line 5: This is what you're asking about, I think. This doesn't compile because this isn't a statement, it' a declaration. The body of an "if" has to be a statement. A block is a statement, so simply enclosing this line in a pair of curly braces will make the code compile. Then, though, the ZipFile will be local to that block -- i.e., not accessible after that block. If you want the ZipFile to be accessible after the block, then you should declare it before the if statement -- i.e.,

Line 6: The ZipFile was never closed.
Line 7: exceptions are ignored (empty catch block). This is a huge no-no Never do this. Always at [i]least[i] print a message to System.out. If you get into this habit, you'll be spending hours and hours debugging things that, if you simply printed exception messages, you'd know about the first time you ran the program.
 
Brahim Bakayoko
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Hi, all! I am new here. <= where are my manners?
2. Thanks for the reply.
3. can you explain a little more why 5 is not a statment:
if(f.isFile())
ZipFile zipFile = new ZipFile(f);
I think I might have skipped over this basic programming notion.
4. I do close all my streams. This was just a simplified code focusing on the specific problem.
5. I have another problem:

The code above is not really able to identify if the ZipEntry (entry) is a directory. Why is that?
Look at the if(entry.isDirectory()){} statement.
Thanks!
[ August 30, 2003: Message edited by: sam thomas ]
 
Brahim Bakayoko
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, here is the full code for the specific function.
I had to add extra codes because it sometimes was not able to detect directories. I guess the entries were not properly set for those zip files as there are different compressions and entry format out there.

[ August 30, 2003: Message edited by: sam thomas ]
[ August 30, 2003: Message edited by: sam thomas ]
[ August 30, 2003: Message edited by: sam thomas ]
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic