So I\'ve been battling this issue for about a week now and think I know the issue but I don\'t want to report an answer there until I have it pegged down.
In a nutshell,
In general, if you have a line of code causing an exception, and there's additional work to be done whether or not the exception occurred, you should just use a catch
, and put the stuff you want done anyway outside (after) the try
/catch
.
For your particular case (described in your other question), you are dealing with the horrible brokenness that is .NET System.IO.Ports.SerialPort
. Opening the serial port succeeded, but SerialPort.Open
is a complicated method that does a bunch of extra stuff, and insists on closing the port if any of that extra (and unnecessary) stuff fails. I've found other problems with System.IO.Ports.SerialPort
as well (e.g. inability to open ports by their internal device name). IMO, the sooner you get rid of System.IO.Ports.SerialPort
the better.
You can either p/invoke the Win32 Serial Port functions (CreateFile
and the rest are here) or find someone who already wrote a wrapper library (I did, but it isn't available publically).