How to ignore an exception AND complete the try

前端 未结 3 671
抹茶落季
抹茶落季 2021-01-27 06:57

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,

3条回答
  •  情深已故
    2021-01-27 07:23

    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).

提交回复
热议问题