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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:41:29+00:00 2026-06-15T11:41:29+00:00

I use property setters to validate the input in a C# class and throw

  • 0

I use property setters to validate the input in a C# class and throw Exceptions on invalid inputs. I also use Json.NET to deserialize a json to an object. The problem is that I don’t know where to catch the exceptions for invalid json values which are thrown by the setters.
The Exception are not thrown from JsonConvert.DeserializeObject method.

public class A{
    private string a;

    public string number{
        get {return a;}
        set {
            if (!Regex.IsMatch(value, "^\\d+$"))
                throw new Exception();
            a = value;
        }
    }
}

public class Main
{
    public static void main()
    {
         // The Exception cannot be caught here.
         A a = JsonConvert.DeserializeObject<A>("{number:'some thing'}");
    }    
}
  • 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-15T11:41:30+00:00Added an answer on June 15, 2026 at 11:41 am

    You need to subscribe to errors while deserializing your object:

                JsonConvert.DeserializeObject<A>("{number:'some thing'}",
                new JsonSerializerSettings
                {
                    Error = (sender, args) =>
                    {
                        Console.WriteLine(args.ErrorContext.Error.Message);
                        args.ErrorContext.Handled = true;
                    }
                });
    

    If you remove args.ErrorContext.Handled = true statement, exception raised in your setter will be rethrown from JsonConvert.DeserializeObject method. It will be wrapped in JsonSerializationException (” Error setting value to ‘number’ “).

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

Sidebar

Related Questions

In my Asp.Net project I wanna use Property Auto-wiring, e.g. for my ILogger. Basically
I'd like to use the image property tag constants defined in GDI+ from .NET.
I have read some about the use of getters/setters in object oriented programming and
I currently use the following syntax to define a class with getters and setters.
I use application property to count number of online users: Application[OnlineUsers] = (int)Application[OnlineUsers] +
I'm want to use RedirectAttibutes property that has come up in Spring 3.1, I
Is it possible to use the property of one Spring bean to set the
I have a bunch of third-party Java classes that use different property names for
The API doesn't show a proper example how to use connectNulls property. I had
You can use the Filter property of a BindingSource to do SQL like filtering.

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.