Catch exception from dll connected using interface not working C#
Frage
I have 3 projects inside my solution.
A: Main program, call plugin using interface.
Plugin plugin = Plugin.GetPluginByName("FILE_ANALYZER");
if (plugin != null)
{
try // show preview of data inside this file using plugin
{
plugin.Interface.Run(forThisFile);
labelResultInfo.Text = plugin.Interface.ResultText;
}
catch // (Exception mainEx)
{
error = true;
}
}
B: DLL with interface for plugins + all "universal" methods which can plugin use for work. This class using methods from epplus dll.
public bool LoadFile()
{
if (!this.FileInfoInitialized.Exists) { return false; }
try
{ // load new excel package from existed file
this.Package = new ExcelPackage(this.FileInfo); // using epplus
return true;
}
catch { return false; }
}
C: My plugin DLL, which implement interface defined in B and call B methods
public bool Run(string fileToAnalyze = "")
{
var rep = ExcelReport(fileToAnalyze);
if (rep.LoadFile())
{
ResultText = "Correct XLSX file";
return true;
}
else
{
ResultText = "Incorrect file, but you can continue without error";
return false;
}
}
Now, I can load plugin without any error, also I can call plugin.Interface.Run(forThisFile);
from program A. If I send incorrect file, I expect that the program declares that the format is incorrect but allow me to continue without error. But not. The program executes the steps in the following order:
1. plugin.Interface.Run(forThisFile); // A // is called with "Not_excel_file.txt"
2. var rep = ExcelReport(fileToAnalyze); // C // is initialized
3. rep.LoadFile() ==> LoadFile() // C ==> B // method from DLL B is called
4. this.Package = new ExcelPackage(this.FileInfo); // B // throw an Exception
>> epplus: File contains corrupted data.
5. catch from LoadFile() return false; // B // exception is catched inside B and return false to C
6. ResultText = "Incorrect file, but you can continue without error"; // C // "if (rep.LoadFile())" in C evaluate "false result" correct way
7. return false; // C ==> A // and return false to main program
8. // And this step is problem
// Now, after return false from C, debugger stay on row in main program A:
error = true;
// And this row was skipped and never called:
labelResultInfo.Text = plugin.Interface.ResultText;
// If I enable catch mainEx, then this Exception is:
>> Object reference not set to an instance of an object.
// In output window I get this after step 5:
>> A first chance exception of type 'System.IO.FileFormatException' occurred in WindowsBase.dll
>> A first chance exception of type 'System.IO.FileFormatException' occurred in EPPlus.dll
// And this message in actual step:
>> A first chance exception of type 'System.NullReferenceException' occurred in MyInterfaceDLL_B.dll
(Note: this is modified code, my interface is more complex, but the error manifests itself exactly this way)
Can anybody say why?
How I can catch exception in dll and not throw it up into main program?
I know that i can catch in A errors only for this row: plugin.Interface.Run(forThisFile);
and then initialize text in label, but why this not working?
What I tried based on your questions?
- Could you have a null reference in this line
labelResultInfo.Text = plugin.Interface.ResultText;
?
I enabled all Common Language Runtime Exceptions and modify A to show what inside plugin.Interface.ResultText
is. New code for catch in A:
catch // (Exception mainEx)
{
error = true;
labelResultInfo.Text = plugin.Interface.ResultText;
}
After run:
- A first chance exception of type 'System.IO.FileFormatException' occurred in WindowsBase.dll Additional information: File contains corrupted data. If there is a handler for this exception, the program may be safely continued.
- A first chance exception of type 'System.IO.FileFormatException' occurred in EPPlus.dll Additional information: File contains corrupted data. If there is a handler for this exception, the program may be safely continued.
- A first chance exception of type 'System.NullReferenceException' occurred in MyInterfaceDLL_B.dll Additional information: Object reference not set to an instance of an object.
After searching in code. >> This is dispose part of package which was closed after try with incorrect file.. So I modify dispose: if (this.Package != null) this.Package.Dispose();
and all exceptions are catched correct now.
Thanks for help.
Keine korrekte Lösung