0

I have a C# .NET program running in Visual Studio. Its function is to merge PDF files together based on their file names. It works fine for the first 12 or so PDFs but then suddenly exits with code 0 at a certain point. I am not using multi-threading and the PDF library I am using is iTextSharp.

public static string MergePDFs(List<string> fileNames, ref string targetPdf)
{
    string merged = "";
    using (FileStream stream = new FileStream(targetPdf, FileMode.Create))
    {
        Document document = new Document();
        PdfCopy pdf = new PdfCopy(document, stream);
        PdfReader reader = null;
        try
        {
            document.Open();
            foreach (var file in fileNames)
            {
                reader = new PdfReader(file);
                pdf.AddDocument(reader);
                reader.Close();
            }
        }
        catch (Exception)
        {
            // merged = "false";
            if (reader != null) reader.Close();
        }
        finally
        {
            if (document != null)
            {
                Console.WriteLine("Closing document.");
                document.Close(); // exits here
                Console.WriteLine("Document closed.");
            }
        }
    }
    return merged;
}

The output in the debugger is:

...
Closing document.
The program has exited with code 0 (0x0)
Daxtron2
  • 1,239
  • 1
  • 11
  • 19
Conor McCauley
  • 125
  • 1
  • 2
  • 16
  • Are the files you are merging large? Maybe it is weird error and you are getting out of memory exception – Brad May 24 '18 at 12:39
  • 1
    Run it under the debugger, or put a try/catch around the code and dump out any exception. – Matthew Watson May 24 '18 at 12:41
  • 6
    always log exceptions, in your case, write the exception to the console – Bart De Boeck May 24 '18 at 12:43
  • @Brad the files never exceed 1.7 megabytes in size. – Conor McCauley May 24 '18 at 12:44
  • Ok, was a thought need more info and full code and stepping through it – Brad May 24 '18 at 12:45
  • @mjwills there are no try/catch statements further up the stack. I will edit my question and provide an MCVE. – Conor McCauley May 24 '18 at 12:46
  • 3
    Write out the exception! – TheSoftwareJedi May 24 '18 at 12:55
  • 2
    If you remove your `try catch` and want to expose the error code of the original exception, https://stackoverflow.com/questions/35294313/exit-code-when-unhandled-exception-terminates-execution may be worth a read. – mjwills May 24 '18 at 12:55
  • Please use Exception ex in your catch-statement and do a Console.WriteLine(ex.Message); inside of your catch-brackets. Afterwards tell us what message is displayed. Otherwise we will only be able to do wild guesses – BotMaster3000 May 24 '18 at 12:59
  • A return code of zero usually indicates success. Is there an exception? The output that you show is from the finally statement, which executes regardless of success or an exception. – Mark Willis May 24 '18 at 13:01
  • 1
    The fact that `Console.WriteLine("Document closed.");` doesn't execute almost guarantees that an exception was thrown @Mark. – mjwills May 24 '18 at 13:02

1 Answers1

1

Printed out the exception via the catch block but it was never reached. The try block executed successfully. Removing the try/catch/finally blocks 'fixed' the issue.

This is only a partial fix as there is no longer a way to handle errors now.

public static string MergePDFs(List<string> fileNames, ref string targetPdf)
{

    string merged = "";

    using (FileStream stream = new FileStream(targetPdf, FileMode.Create))
    {

        Document document = new Document();
        PdfCopy pdf = new PdfCopy(document, stream);
        PdfReader reader = null;

        document.Open();

        foreach (var file in fileNames)
        {
            reader = new PdfReader(file);
            pdf.AddDocument(reader);
            reader.Close();
        }

        document.Close();

    }

    return merged;

}
Conor McCauley
  • 125
  • 1
  • 2
  • 16