C# try/catch not work

hossein tavakoli 471 Reputation points
2023-06-13T07:35:48.4466667+00:00

I'm using WPF/C# .NET 6.0.9

I have a class class library DLL file that contain a method, I use the method with try/catch but it does not work, then application will crash and exit.

try 
{
   var result = PosAtmDevice.PosAtmPayment(Convert.ToDecimal(100000), posVendor, connectionType, selectedPos.IpAddress, selectedPos.PortNumber, selectedPos.PortName, int.Parse(selectedPos.PortBitrate), selectedPos.TimeOut,                     PosAtmDevice.RunTypes.Sync, selectedPos.PosId, selectedPos.TerminalId, selectedPos.AcceptorId, selectedPos.TextOnReceipt, deviceType);
}
catch (Exception ex)
{
   MessageBox.Show(ex.Message);
}

Error is :

Symbol loading for optimized module System.IO.Ports.dll was skipped because Just My Code is enabled Disassembly 1ADB3EAF mov ecx,dword ptr [edx+4] System.NullReferenceException: 'Object reference not set to an instance of an object.'

After disable Enable Just my code and Show disassembly if source is not available in Option > Debugging , this error will happen

System.Io.Ports.pdb Not loaded System.IO.Ports.pdb contains the debug information required to find the source for the module System.IO.Ports.dll

Version : 7.00.22.51805 Location : D:\Proj c\StoreManager\StoreManager\bin\Debug\net6.0-windows\runtimes\win\lib\net6.0\System.IO.Ports.dll

but Catch does not work , and my app will crash.

Developer technologies | Windows Presentation Foundation
Developer technologies | .NET | Other
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 81,696 Reputation points Volunteer Moderator
    2023-06-13T15:15:56.05+00:00

    As suggested most likely your code is calling a dll that is designed for the 4.8 runtime and will not work .net 6 as the runtimes are not compatible, thus the try/catch not working.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.