Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7820211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:12:29+00:00 2026-06-02T07:12:29+00:00

I have need of a .net structure (it mimics a connected devices internal map)

  • 0

I have need of a .net structure (it mimics a connected devices internal map) and I want to use a try catch block since I’m using Marshall.PtrToStructure() and associated GChandle stuff. However I when I put the structures field assignments inside the try catch block I get this error “field1 must be fully assigned before control is returned to sender”. The basic code works fine without the try catch block. Is there any way around this error while using try catch blocks? Should I use try catch?

[StructLayout( LayoutKind.Sequential )]
public struct Effects
{
    public UInt16 field_1;
    public UInt16 field_2;
    ...



    public Effects(byte[] effectsData)
    {
       GCHandle gch;
       try
       {
           gch = GCHandle.Alloc( effectsData, GCHandleType.Pinned );
           IntPtr pEffects = gch.AddrOfPinnedObject( );
           this = (Effects)Marshal.PtrToStructure( pEffects, typeof(Effects ) );
       }
       catch (Exception ex)
       {

       }
       finally
       {
           if (gch.IsAllocated)
               gch.Free( );
       }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:12:31+00:00Added an answer on June 2, 2026 at 7:12 am

    The constructor must guarantee that if it returns normally then all the fields are filled in.

    Suppose your code throws an exception on the first line of the try. Then you catch the exception, eat it, and return, without having ever filled in any of the fields! The compiler detects this and disallows the program.

    Eating every exception is almost certainly the wrong thing to do here. Do you really want to return an uninitialized structure if there was an unhandled and unexpected arbitrary exception?

    If that is what you want to do then you can simply say:

    public Effects(byte[] effectsData) : this() {
    

    that will guarantee that the fields are initialized to their default values before the ctor block runs.

    But again: is that really what you want to do? This code looks incredibly dangerous to me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need to have one project on asp.net mvc 1 but i want to
I have a need to allow my users to execute .NET code that they
We have a need to access kernel global vars in net/ipv4/af_inet.c that are not
Retrofitting ASP.NET WebForms themes to an old application I have a need to theme
I am working on a ASP.NET app and i have a need to post
I'm building a .NET 3.5 application and have the need to evaluate JS code
I've got a VB.NET WinForms app in which I have the need to refer
I am developing an application in asp.net mvc where i need to have Chat
On large-scale Java/.Net Enterprise projects, does every developer need to have all the components/libraries/dependencies
I have some .net apps running that I need to monitor for example, then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.