0

How can I catch all exceptions in C# from libtiff.net TIFFCP.exe I would like to get exceptions from TTIFFCP.exe(Merge, Split) such as DirectoryNotFoundException, FileNotFoundException ... I can see these errors on visula studio debugger window but it does not pass catch block.

I tried like these(I made an error deliberately)

<pre><code>
<hr/>

Code A : 

string[] arguments = 
{
    @"Sample Data\EEEEEEE.tif",
    @"Sample Data\marbles.tif",
    "MergeTiffImages.tif"
};
TiffCP.Program.Main(arguments);

===> nothing return

<hr/>

Code B :

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = @"TiffCP.exe";
string path1 = @"Sample Data\EEEEEEE.tif";
string path2 = @"Sample Data\marbles.tif";
string path3 = "MergeTiffImages.tif";
p.StartInfo.Arguments = "\"" + path1 + "\"" + ", \"" + path2 + "\"" + ", \"" + path3 + "\"";
p.Start();
string t = p.StandardOutput.ReadToEnd();

===> string t => ""

<hr/>

Code C :

Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(@"TiffCP.exe");
string path1 = @"Sample Data\EEEEEEE.tif";
string path2 = @"Sample Data\marbles.tif";
string path3 = "MergeTiffImages.tif";
myProcessStartInfo.StartInfo.Arguments = "\"" + path1 + "\"" + ", \"" + path2 + "\"" + ", \"" + path3 + "\"";
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardError;
string t = myStreamReader.ReadLine();
myProcess.Close();
===> string t => Open: Failed to open "Sample Data\EEEEEEE.tif" 

</code></pre>

but it does not pass catch block

Is it possible to get Errors from TIFFCP.exe? thanks in advance.

  • Are these "*first chance exceptions*" by any chance – TheGeneral Oct 30 '18 at 04:35
  • 1
    It won't trigger an exception because your process is working correctly, without error. Faulting applications should return a non-zero exit code. – ProgrammingLlama Oct 30 '18 at 04:40
  • 1
    Possible duplicate of [How to catch exceptions from processes in C#](https://stackoverflow.com/questions/320767/how-to-catch-exceptions-from-processes-in-c-sharp) – ProgrammingLlama Oct 30 '18 at 04:42
  • [Secondary duplicate](https://stackoverflow.com/questions/2279181/catch-another-process-unhandled-exception) which looks at launching external .NET executables within the same process, and catching their exceptions that way. – ProgrammingLlama Oct 30 '18 at 04:43
  • Thanks provide me a lot of information. I will take "copy original code". I think it's easy for me. – user9591156 Oct 31 '18 at 00:36

1 Answers1

0

As I can see you tried two different methods. As others said, Code B won't give you any exception as you can't catch those of a different process.

Code A if you look at the source returns nothing. So it is normal. From there you have two choices: either you copy the code from the main method of this source and manage yourself exceptions OR you get the output and manage errors from there.

Master DJon
  • 1,899
  • 2
  • 19
  • 30
  • Thanks Master. I understand that it can't catch error from different process. I search and found source of TIFFCP.exe as you mentioned. Then I including two files to my Project and run this Project. But still can't catch error because On the Code like this using (Tiff inImage = Tiff.Open(fileAndPageNums[0], "r")) { if (inImage == null) return; if it could not find files return null.... I think I have to little more fix these files but I can see the light at the end. Thank you anyway. – user9591156 Oct 31 '18 at 00:35
  • You are welcome. If and only if this answer fits your need, you shall accept it. – Master DJon Oct 31 '18 at 02:32