简体   繁体   中英

UnauthorizedAccessException in serialPort

i am doing C# coding on COM port, i was unable to understand the meaning of this exception, i try to find the answer in my book and web, but no good news.

So my question is how to find the definition of the right exception name(I copy this code from web). 2. what's this ex? an object of the exception? 3. Do I need to add anything to Message?

Thanks for your replies.

code is very simple:

internal void OpenPort(string portName)
{
    try
    {
        myComPort.PortName = portName;
        myComPort.Open();
    }
    catch (UnauthorizedAccessException ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally { }
}

Change your code from "UnauthorizedAccessException ex" to "Exception ex", stick a breakpoint in the catch and see what exception type is being thrown. In Java you should catch the specific exception type, in C# it's less important, you can get all of the information you need from the generic Exception class, without catching the other exception types! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM