Bug 15514 - Try/catch does not catch SerialPort.Open() exception.
Summary: Try/catch does not catch SerialPort.Open() exception.
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: 3.2.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2013-10-19 13:55 UTC by Jonathan Lee
Modified: 2014-08-29 15:06 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

command line program source (476 bytes, application/octet-stream)
2013-10-19 13:55 UTC, Jonathan Lee

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Jonathan Lee 2013-10-19 13:55:14 UTC
Created attachment 5179 [details]
command line program source

Opening a non-existent serial port should understandably cause an exception, however, the try/catch mechanism was not able to catch what appears to be an extra exception which causes an ugly error message.

The behavior on Windows .NET is a clean exit from the program with no errors, however, Mac OS X spews this:

Error opening COM port.

Unhandled Exception:
System.IO.IOException: Bad file descriptor
  at System.IO.Ports.SerialPortStream.ThrowIOException () [0x00000] in <filename unknown>:0
  at System.IO.Ports.SerialPortStream.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0
  at System.IO.Ports.SerialPortStream.Finalize () [0x00000] in <filename unknown>:0

The fact that it printed from my catch block suggests that it caught one exception but then an extra one is thrown that I don't know how to catch.  Source code is attached.

Command line repro:  mono hello_world.exe <path to serial port>

Simply put in a non-existent serial port above to repro.
Comment 1 Zoltan Varga 2013-10-20 02:44:20 UTC
The exception is thrown from the finalizer on the finalizer thread this is why the try-catch can't catch it.
-> class libs.
Comment 2 Murz 2014-08-29 14:18:16 UTC
I got the same problem, is there any solutions for this issue?
Comment 3 Zoltan Varga 2014-08-29 15:06:52 UTC
Fixed in mono master bfc3336224745663aaf6bd962684549b9f783241.