Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Unmanaged Exception Handling


  • Please log in to reply
8 replies to this topic

#1 Charles

Charles

    Advanced Member

  • Members
  • PipPipPip
  • 192 posts

Posted 01 January 2011 - 06:36 AM

Is it possible to catch an exception thrown in unmanaged code with managed code??

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 01 January 2011 - 06:57 AM

Hi Charles, In a way. To pass an exception to managed code, set hr to one of the .NET MF exception constants. This will be converted into the appropriate Exception on the managed side... Chris

#3 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 01 January 2011 - 08:45 AM

Is it possible to catch an exception thrown in unmanaged code with managed code??

You should be able to catch unmanaged exception with a general catch block (a catch block without an exception type specified):

try
{
...
}
catch
{
  // Catches non-CLS exceptions
}
Unfortunately, you cannot access the exception details, you'd need runtime support for that (e.g. some special method). What unmanaged exceptions are you interested in? I don't think .NET Micro Framework is compiled with (C++) exceptions enabled.

#4 Charles

Charles

    Advanced Member

  • Members
  • PipPipPip
  • 192 posts

Posted 02 January 2011 - 05:25 PM

What unmanaged exceptions are you interested in?


I'm trying to make the N+ automatically reboot when the ethernet cable gets bumped and the unmanaged code crashes... Or any other time something goes terribly wrong - I want the unit to be able to automatically recover from various unanticipated error conditions.

#5 Charles

Charles

    Advanced Member

  • Members
  • PipPipPip
  • 192 posts

Posted 02 January 2011 - 05:27 PM

Hi Charles,

In a way. To pass an exception to managed code, set hr to one of the .NET MF exception constants. This will be converted into the appropriate Exception on the managed side...

Chris



That flew right over my head and possibly broke a window... :D

What is hr? Where would I find some good reading on .NET MF exception constraints?

#6 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 02 January 2011 - 07:18 PM

I'm trying to make the N+ automatically reboot when the ethernet cable gets bumped and the unmanaged code crashes... Or any other time something goes terribly wrong - I want the unit to be able to automatically recover from various unanticipated error conditions.


Would it be helpful if we exposed a SoftReboot and/or HardReboot method for those "something goes terribly wrong" cases?

You can also wire a GPIO to the RESET header and drive it low to reset the board.

Chris

#7 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 02 January 2011 - 07:26 PM

That flew right over my head and possibly broke a window... :D

What is hr? Where would I find some good reading on .NET MF exception constraints?


:)

The official Microsoft reference to the porting guide is installed with the porting kit (.NET MF source).

Look for C:\MicroFrameworkPK_v4_1\Documentation\RCLPort.chm. Except follows.

RCLPort.chm > The .NET Micro Framework Porting Kit > Porting Kit Overviews > Adding Features to Your Port > Adding Features to the .NET Micro Framework by Interoperating with Unmanaged Code > Creating Interop APIs > Writing Your Interop Drivers
"Your stubs have an HRESULT reference parameter that they can use to pass error codes back to your C# interop methods. These are translated to exceptions that are thrown in the C# interop method. The following table lists the C++ error codes that are supported and their corresponding exceptions."

The table lists possible C++ error codes to return... For instance, setting hr to CLR_E_ARGUMENT_NULL results in the .NET MF runtime throwing a System.ArgumentNullException.

Chris

#8 Charles

Charles

    Advanced Member

  • Members
  • PipPipPip
  • 192 posts

Posted 03 January 2011 - 05:15 AM

Would it be helpful if we exposed a SoftReboot and/or HardReboot method for those "something goes terribly wrong" cases?
Chris


That would be of great help as it would prevent me from having to waste a GPIO.

#9 phantomtypist

phantomtypist

    Advanced Member

  • Members
  • PipPipPip
  • 142 posts
  • LocationNew York, NY

Posted 03 January 2011 - 06:14 AM

It would be even better if we could have a watchdog timer implemented.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.