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 9134447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:39:38+00:00 2026-06-17T08:39:38+00:00

I am trying to add some error-handling to a block of code in the

  • 0

I am trying to add some error-handling to a block of code in the Effort library which generates an expression tree to perform a conversion and assign that result to a property.

The problem with the existing code is that a NullReferenceException is thrown when this expression is invoked at runtime when trying to assign null to a property with a value type. In this case I have no information about the property it was attempting to assign, so I want to throw a more specific exception.

Following is my first attempt to just encapsulate this logic in a try/catch block, and throw an exception if the conversion fails. Eventually I would add more information to the InvalidOperationException.

blockElements.Add(
    Expression.TryCatch(
        Expression.Assign(
            Expression.Property(result, this.Properties[i]),
            Expression.Convert(
                Expression.ArrayIndex(parameter, Expression.Constant(i)),
                this.Properties[i].PropertyType)),
        Expression.Catch(typeof(NullReferenceException),
            Expression.Throw(Expression.Constant(
            new InvalidOperationException("Unhandled exception"))))));

In my mind this is what I’m trying to do:

try
{
    Property = (int)value;
}
catch (NullReferenceException)
{
    throw new InvalidOperationException("Unhandled exception");
}

However at runtime, that expression now throws an ArgumentException with the message “Body of catch must have the same type as body of try.” What am I doing wrong here? Do I need to create a Block in the Catch expression to “return” some dummy value, even though it will never be hit due to the Throw?

Or am I approaching this in entirely the wrong way?

  • 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-17T08:39:39+00:00Added an answer on June 17, 2026 at 8:39 am

    In normal C# code, a method as a whole has to either return a value or throw an exception.

    With Expressions, it works a bit different: each expression has a return type, and in the case of TryCatch, the return type of the try Expression has to be the same as the return type of any of the catch Expressions.

    In your case, the type of the try is int, but the type of the catch is void, so they can’t be used together. To fix this, you need to either change the type of the try to void, or change the type of the catch to int.

    To change the type of the try to void, you can use an overload of Expression.Block() that lets you specify the type of the block (normally, it’s the same as the type of the last expression in the block):

    Expression.TryCatch(
        Expression.Block(
            typeof(void),
            Expression.Assign(…)),
        Expression.Catch(
            typeof(NullReferenceException),
            Expression.Throw(
                Expression.Constant(
                    new InvalidOperationException("Unhandled exception")))))
    

    To change the type of the catch to int, you would somehow need to change the type of the Throw expression. And since for a Throw expression, any return type can be valid (because it doesn’t actually return), there is an overload that lets you specify the return type:

    Expression.TryCatch(
        Expression.Assign(…),
        Expression.Catch(
            typeof(NullReferenceException),
            Expression.Throw(
                Expression.Constant(
                    new InvalidOperationException("Unhandled exception")),
                typeof(int))))
    

    I think changing the type of the try is conceptually clearer, because you don’t actually want to return anything from the whole expression.

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

Sidebar

Related Questions

I am trying to add some more native error handling to an SSIS C#
I am trying to add some error detection to a script that is used
I was trying to add some non-production test code by creating a 3rd partial
I am trying to add some views programatically to a linear layout which is
I'm getting the following error when trying to add some data from myXml.xml to
I'm trying to add some code in a presentation made with LaTex. I used
I'm trying to add some error checking inside my PHP script. Is it valid
I'm trying to add some extended error codes to the event log but I
There is a podcast that I'm trying to add to itunes which is handling
Im trying to add some callback to my code, so when clicking on a

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.