Skip to Content
NexPDF

Error handling

The exceptions NexPDF for Java throws, the engine's error messages, license error codes, and patterns for processing untrusted files.

English

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

ExceptionThrown when
NexPdfExceptionAn engine call failed. getMessage() is the engine's message. Unchecked (extends RuntimeException).
NexPdfException with code() < 0NexPdf.activateLicense rejected the key. For every other failure code() is 0.
IOExceptionNexPdfDocument.open(Path) could not read the file, or save(Path) could not write it.
IllegalStateExceptionA method was called on a closed NexPdfDocument ("document is closed").
UnsatisfiedLinkErrorJNA 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

MessageCauseWhat to do
password requiredThe file is encrypted and no password was givenAsk for the password and pass it to open(path, password)
incorrect passwordThe password matches neither the user nor the owner passwordAsk again
no license activated: '<module>' requires a NexPDF license keyThe call needs a module and no key is activeCall activateLicense at start-up; see Running without a key
the active license does not include the '<module>' moduleYour plan does not include the moduleCheck NexPdf.isLicensed(module), or upgrade the plan
internal error: ...The engine caught an unexpected failure on this inputThe 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:

CodeMeaning
-1Malformed key
-2Bad signature (the key was changed, or it is not a NexPDF key)
-3Unsupported key version (update the SDK)
-4Expired
-5Not yet valid (check the system clock)
-6Wrong platform
-7Wrong 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 NexPdfException per 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 a byte[]);
  • 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();
    }
}

On this page