Error handling
The exceptions NexPDF for Java throws, the engine's error messages, license error codes, and patterns for processing untrusted files.
Every NexPDF call either returns its result or throws. There are no status codes to check by hand: the SDK reads the engine's per-thread error after each call and turns it into an exception.
Exception types
| Exception | Thrown when |
|---|---|
NexPdfException | An engine call failed. getMessage() is the engine's message. Unchecked (extends RuntimeException). |
NexPdfException with code() < 0 | NexPdf.activateLicense rejected the key. For every other failure code() is 0. |
IOException | NexPdfDocument.open(Path) could not read the file, or save(Path) could not write it. |
IllegalStateException | A method was called on a closed NexPdfDocument ("document is closed"). |
UnsatisfiedLinkError | JNA could not find or load the native library, on the first NexPDF call. |
try (NexPdfDocument doc = NexPdfDocument.open(Path.of("upload.pdf"))) {
doc.addWatermark("CONFIDENTIAL");
doc.save(Path.of("out.pdf"));
} catch (NexPdfException e) {
log.warn("NexPDF could not process upload.pdf: {}", e.getMessage());
} catch (IOException e) {
log.warn("cannot read or write the file", e);
}NexPdfException is unchecked, so it passes through lambdas and streams. Catch it where you can
report the failure, for example per file in a batch.
Common engine messages
| Message | Cause | What to do |
|---|---|---|
password required | The file is encrypted and no password was given | Ask for the password and pass it to open(path, password) |
incorrect password | The password matches neither the user nor the owner password | Ask again |
no license activated: '<module>' requires a NexPDF license key | The call needs a module and no key is active | Call activateLicense at start-up; see Running without a key |
the active license does not include the '<module>' module | Your plan does not include the module | Check NexPdf.isLicensed(module), or upgrade the plan |
internal error: ... | The engine caught an unexpected failure on this input | The JVM keeps running; report the file to support |
Other messages describe the input: a malformed or truncated PDF, a page index out of range, invalid JSON options. They are meant to be logged as they are.
License error codes
NexPdfException.code() after activateLicense is one of:
| Code | Meaning |
|---|---|
| -1 | Malformed key |
| -2 | Bad signature (the key was changed, or it is not a NexPDF key) |
| -3 | Unsupported key version (update the SDK) |
| -4 | Expired |
| -5 | Not yet valid (check the system clock) |
| -6 | Wrong platform |
| -7 | Wrong application id |
A rejected key leaves the previous license state as it was, so a process that had no key stays in
view-only mode. See License for a switch on the code.
The last error
NexPdf.lastError() returns the engine's last error message for the current thread, or null. You
rarely need it, because the SDK throws with that message already, but it is useful after
NexPdf.invoke, which reports failures inside its JSON result instead of throwing.
Untrusted input
The engine is written in Rust and fuzzed; a malformed PDF produces a NexPdfException, not a crash
of the JVM. When you process uploads:
- catch
NexPdfExceptionper file, so one bad file does not stop a batch; - limit the file size you accept before opening it, since documents are read into memory
(
open(Path)reads the whole file into abyte[]); - close documents with try-with-resources so native memory is released on failure too.
static OptionalInt pageCount(byte[] data) {
try (NexPdfDocument doc = NexPdfDocument.openBytes(data)) {
return OptionalInt.of(doc.pageCount());
} catch (NexPdfException e) {
return OptionalInt.empty();
}
}