Detect File Format & Throw UnsupportedFileFormatException when Opening an Invalid PDF File with Aspose.Words for .NET C# API

Hi
We’re having some unit tests which ran through without trouble on
the Aspose.Words 10 BETA version but have one that fails in 10.0.1:
When we do:

new AsposeWordFile(getClass().getResourceAsStream("/some.pdf"));

We’d expect an IOException because that file is no valid DOC (it’s a valid PDF), but instead we get:

java.lang.Exception: Unexpected exception, expected <java.io.IOException> but was <java.util.ServiceConfigurationError>
Caused by: java.util.ServiceConfigurationError: java.nio.charset.spi.CharsetProvider: Provider com.aspose.ms.java.utf7charset.provider.CharsetProvider not found
at java.util.ServiceLoader.fail(ServiceLoader.java:231)
at java.util.ServiceLoader.access$400(ServiceLoader.java:181)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:367)
at java.util.ServiceLoader$1.next(ServiceLoader.java:438)
at java.nio.charset.Charset$1.getNext(Charset.java:338)
at java.nio.charset.Charset$1.hasNext(Charset.java: 351)
at java.nio.charset.Charset$2.run(Charset.java: 394)
at java.nio.charset.Charset$2.run(Charset.java: 392)
at java.security.AccessController.doPrivileged(Native Method)
at java.nio.charset.Charset.lookupViaProviders(Charset.java: 391)
at java.nio.charset.Charset.lookup2(Charset.java: 468)
at java.nio.charset.Charset.lookup(Charset.java: 456)
at java.nio.charset.Charset.forName(Charset.java: 521)
at asposewobfuscated.rv.pl(Encoding.java: 502)
at asposewobfuscated.rv.pi(Encoding.java: 468)
at asposewobfuscated.rv.a(Encoding.java: 452)
at asposewobfuscated.rv.m(Encoding.java: 392)
at com.aspose.words.pa.a(FileFormatDetector.java: 318)
at com.aspose.words.pa.aN(FileFormatDetector.java: 304)
at com.aspose.words.pa.A(FileFormatDetector.java: 44)
at com.aspose.words.Document.b(Document.java: 1108)
at com.aspose.words.Document.a(Document.java: 1084)
at com.aspose.words.Document.(Document.java: 194)
at com.aspose.words.Document.(Document.java: 165)
at com.aspose.words.Document.(Document.java: 160)

Could you have a look at it?
Thanks

Matt Keller

Hi
Thanks for your request. I managed to reproduce the problem on my side. You are right, there should be UnsupportedFileFormatException. I logged this problem into our defect database. We will let you know once it is resolved.
Best regards,

Thanks
But please either throw some subclass of IOException (preferred) or clearly document that new exception in the javadoc and release notes.
The current javadoc states:
Throws:

IOException - if the stream could not be read or does not contain a Word document

Thanks
Matt

Hi Matt,
Thanks for your request. Andrey is right – UnsupportedFileFormatException should be thrown. Please see the following link for more details:
https://reference.aspose.com/words/java/com.aspose.words/unsupportedfileformatexception/
Since Aspose.Words does not support PDF as input document format – this type of exception should be thrown.
Best regards,

That’s okay with me, just as long as this exception is clearly documented in the javadoc and method signature.
Thanks
Matt

The issues you have found earlier (filed as WORDSJAVA-7) have been fixed in this .NET update and in this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.