Win a copy of Getting started with Java programming language this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

POI: Deciding to run a method from an Exception?  RSS feed

 
Mike London
Ranch Hand
Posts: 1313
6
  • Mark post as helpful
  • send pies
  • 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
 
praveen kumaar
Ranch Hand
Posts: 450
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15816
76
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 1313
6
  • Mark post as helpful
  • send pies
  • 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: 450
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read this→DontUseExceptionsForFlowControl.

Praveen.
 
Mike London
Ranch Hand
Posts: 1313
6
  • Mark post as helpful
  • send pies
  • 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
 
Ron McLeod
Saloon Keeper
Posts: 1349
193
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.

 
Paul Clapham
Sheriff
Posts: 22114
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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?
 
Knute Snortum
Sheriff
Posts: 3446
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!