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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:43:13+00:00 2026-05-10T20:43:13+00:00

while writing a custom attribute in C# i was wondering if there are any

  • 0

while writing a custom attribute in C# i was wondering if there are any guidelines or best practices regarding exceptions in attributes. Should the attribute check the given parameters for validity? Or is this the task of the user of the property?

In a simple test I did the exception was not thrown until i used GetCustomAttributes on a type with an exception throwing attribute. I just think it’s a bit awkward to get an exception from an Attribute only when explicitly asking for them.


Example Attribute with exception:

[AttributeUsage(AttributeTargets.Interface, AllowMultiple = false, Inherited = false)] sealed public class MyAttribute : Attribute {     public string SomeValue { get; private set; }      public MyAttribute(string someValue)     {         if(string.IsNullOrEmpty(someValue))         {             throw new ArgumentNullException('path');         }          if(!someOtherCheck(someValue))         {             throw MyAttributeException('An other error occured');         }          SomeValue = someValue;     } } 
  • 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. 2026-05-10T20:43:14+00:00Added an answer on May 10, 2026 at 8:43 pm

    Attributes are only actually constructed when you use reflection, so that’s the only time you can throw an exception. I can’t remember ever using an attribute and having it throw an exception though. Attributes usually provide data rather than real behaviour – I’d expect the code which uses the attribute to provide any validation. I know this isn’t like normal encapsulation, but it’s the way it tends to be in my experience.

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

Sidebar

Related Questions

I am using wpf Custom Commands. While writing a custom Command, one need to
While writing a card shuffling algorithm I realized that there are 52! ~= 2^225
There are certain guide lines to be followed while writing Html for you page.
I'm in the process of writing a custom heatmap generator. I'm wondering what the
I'm in the process of writing a custom drop down button in silverlight. While
While writing a custom import tool for a Tridion 2011 project using the core
I've come across a problem while writing a custom Adapter for the items on
While writing this method for a custom NUnit Constraint. private void AddMatchFailure<TExpected, TActual>(string failureName,
I'm writing a custom tunnel (first there's a custom hello and then the connection
While writing some C code, I decided to compile it to assembly and read

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.