There isn't a general way to handle gracefully an error in the document()
function.
According to the XSLT 1.0 spec:
" If there is an error retrieving the resource, then the XSLT processor may signal an error; if it does not signal an error, it must recover by returning an empty node-set. "
This means that we are at the mercy of the implementor whether an empty node-set is produced by the function (good, we can test for empty (non-existent) node-set) or "signals an error", which typically may end the transformation.
In case we have checked that a particular implementation of a specific XSLT processor only produces an empty node-set and does not end the transformation, we may decide to test for this condition and "gracefully" recover. However, our application becomes non-portable, it depends on this specific XSLT processor and there is absolutely no guarantee that in the next version this behaviour will not change to the worse one. Risky, isn't it?
Therefore, it is best that whoever launches the transformation (such as from within a C# program), should check for the existence of the file and pass an appropriate parameter to the transformation, reflecting this existence.