• 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

POI: Deciding to run a method from an Exception?

 
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As useful as they are, I haven't found a way using the Apache POI classes to give me a way to tell if I have a DOCX (XML format) or DOC (OLE format).

Thus, running the POI logic, you can get a particular document Exception if you're using the wrong POI classes for the Word document format you're trying to read.

Relying on the file name extension might work most of the time, but a user could change the file extension from .DOCX -> .DOC or vice versa which wouldn't change the document format.

My question, then, is ... is it "bad form" to, say assume one document format, but if the document exception gets thrown, to then just call another method to use the correct classes?

This is a really a generic question: Is it allowable programming practice to use some exception to decide to run another method -- WHEN, for example in this case, I don't see how to determine the format of a Word document without first trying to, well, use it.

Thanks in advance,

-- mike
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have not any experience with Apache Poi but it seems,as per my understanding,you meant to call an another method if the method it is contained in throws an exception.
if it is..then yes it is allowed(either bad or good depends upon the exceptional situation),you can catch the exception and call the other one...
i can only help this much...
may be it is unclear due to the mentioned reasons..but you can wait till some other will reply.there are plenty of experienced POI users on ranch...

Kind regards,
Praveen
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In general, using exceptions for flow control (that is, using it instead of for example an if-statement, in the way you describe) is regarded as bad practice by many programmers; but sometimes there's no other way to do it, or the alternatives are even worse. I don't have so much experience with Apache POI that I can immediately tell if there's a way to distinguish between docx and doc files.

Mike London wrote:Relying on the file name extension might work most of the time, but a user could change the file extension from .DOCX -> .DOC or vice versa which wouldn't change the document format.


That sounds like an esoteric use case, something that user's don't normally do (who would deliberately change the extension of a .docx file to .doc?). I would consider it the user's own fault if (s)he does this, and not the responsibility of my program to be able to handle this.
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:In general, using exceptions for flow control (that is, using it instead of for example an if-statement, in the way you describe) is regarded as bad practice by many programmers; but sometimes there's no other way to do it, or the alternatives are even worse. I don't have so much experience with Apache POI that I can immediately tell if there's a way to distinguish between docx and doc files.

Mike London wrote:Relying on the file name extension might work most of the time, but a user could change the file extension from .DOCX -> .DOC or vice versa which wouldn't change the document format.


That sounds like an esoteric use case, something that user's don't normally do (who would deliberately change the extension of a .docx file to .doc?). I would consider it the user's own fault if (s)he does this, and not the responsibility of my program to be able to handle this.



Gotcha. Makes total sense. Thanks very much.
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can read this→DontUseExceptionsForFlowControl.

Praveen.
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

praveen kumaar wrote:I have not any experience with Apache Poi but it seems,as per my understanding,you meant to call an another method if the method it is contained in throws an exception.
if it is..then yes it is allowed(either bad or good depends upon the exceptional situation),you can catch the exception and call the other one...
i can only help this much...
may be it is unclear due to the mentioned reasons..but you can wait till some other will reply.there are plenty of experienced POI users on ranch...

Kind regards,
Praveen



Thanks Praveen. I was just doing a sanity check since I've never run into that case.

Appreciate your reply.

mike
 
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you will find that docx files (probably all the x files) have a magic number of 50 4B 03 04 at the beginning of the file.

 
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
I was under the impression that the newer Apache POI classes for Word, the ones which can handle DOCX files, can also handle DOC files. Is this not correct?

In other words, are you just assuming that you have to make the DOCX versus DOC decision yourself, or have you actually found by testing that you have to do that?
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

POI Documentation wrote:HWPF is the name of our port of the Microsoft Word 97(-2007) file format to pure Java. It also provides limited read only support for the older Word 6 and Word 95 file formats.

The partner to HWPF for the new Word 2007 .docx format is XWPF. Whilst HWPF and XWPF provide similar features, there is not a common interface across the two of them at this time.


So I think we can assume that there is no POI interface for both .doc and .docx.
 
reply
    Bookmark Topic Watch Topic
  • New Topic