3

Some programming languages have exception handling structures, namely try-catch blocks. One that I know to give an example it Matlab. Consider the following pseudocode masterpiece:

try
    command_that_will_throw_error
catch the_error
    disp('The command_that_will_throw_error has finished unexpectedly! D=')
end

disp('But the show can never stop!')

The program tries to execute the command_that_will_throw_error, fails, stores information about the error in the_error, and then passes the execution to the catch part of the block, so the result of the code above will be:

The command_that_will_throw(error) has finished unexpectedly! D=

But the show can never stop!

My curiosity induced question is: can (La)TeX do something like this?

My guess is that the answer is no, but the last time I said this, @egreg proved me wrong, so...


Before someone asks, what I'm trying to do is to programatically include some pdf files in my document, some of which may not exist. I would like to make TeX just skip them or print something if said pdf file does not exist.

I accept an answer to my actual problem with the pdf files, but I would to know about the error handling issue for general cases.

1 Answers1

4

To your actual problem: You can use a simple command to include pdf files, which does an existence check for you:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand { \includemypdf } { m }
    {
        \file_if_exist:nTF { #1 }
            {
                % do some cool stuff here
            }
            {
                % maybe issue a warning in the log
            }
    }
\ExplSyntaxOff

\begin{document}
\includemypdf{example-image.pdf}% exists
\includemypdf{quack.pdf}%does not exist
\end{document}
TeXnician
  • 33,589