Error handling
The exceptions NexPDF for .NET 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. Message is the engine's message. |
NexPdfException with Code < 0 | NexPdfSdk.ActivateLicense rejected the key. For every other failure Code is 0. |
IOException, FileNotFoundException, UnauthorizedAccessException | NexPdfDocument.Open(path) could not read the file, or Save(path) could not write it. |
ObjectDisposedException | A method was called on a disposed NexPdfDocument. |
OverflowException | A negative page index was passed where the engine takes an unsigned index. |
DllNotFoundException | .NET could not find or load the native library, on the first NexPDF call. |
EntryPointNotFoundException | The native library is older than the package and lacks a function. |
try
{
using var doc = NexPdfDocument.Open("upload.pdf");
doc.AddWatermark("CONFIDENTIAL");
doc.Save("out.pdf");
}
catch (NexPdfException e)
{
logger.LogWarning("NexPDF could not process upload.pdf: {Message}", e.Message);
}
catch (IOException e)
{
logger.LogWarning(e, "cannot read or write the file");
}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 NexPdfSdk.IsLicensed(module), or upgrade the plan |
internal error: ... | The engine caught an unexpected failure on this input | The process 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. Exception filters (catch (NexPdfException e) when (e.Code == -4)) let you handle
each code; see License.
The last error
NexPdfSdk.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
NexPdfSdk.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 process. 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[]); - dispose documents with
usingso native memory is released on failure too.
static int? PageCountOrNull(byte[] data)
{
try
{
using var doc = NexPdfDocument.OpenBytes(data);
return doc.PageCount;
}
catch (NexPdfException)
{
return null;
}
}