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

  • Home
  • SEARCH
  • 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 51773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:48:10+00:00 2026-05-10T16:48:10+00:00

Visual Studio Test can check for expected exceptions using the ExpectedException attribute. You can

  • 0

Visual Studio Test can check for expected exceptions using the ExpectedException attribute. You can pass in an exception like this:

[TestMethod] [ExpectedException(typeof(CriticalException))] public void GetOrganisation_MultipleOrganisations_ThrowsException() 

You can also check for the message contained within the ExpectedException like this:

[TestMethod] [ExpectedException(typeof(CriticalException), 'An error occured')] public void GetOrganisation_MultipleOrganisations_ThrowsException() 

But when testing I18N applications I would use a resource file to get that error message (any may even decide to test the different localizations of the error message if I want to, but Visual Studio will not let me do this:

[TestMethod] [ExpectedException(typeof(CriticalException), MyRes.MultipleOrganisationsNotAllowed)] public void GetOrganisation_MultipleOrganisations_ThrowsException() 

The compiler will give the following error:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute

Does anybody know how to test for an exception that has a message from a resource file?


One option I have considered is using custom exception classes, but based on often heard advice such as:

‘Do create and throw custom exceptions if you have an error condition that can be programmatically handled in a different way than any other existing exception. Otherwise, throw one of the existing exceptions.’ Source

I’m not expecting to handle the exceptions differently in normal flow (it’s a critical exception, so I’m going into panic mode anyway) and I don’t think creating an exception for each test case is the right thing to do. Any opinions?

  • 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-10T16:48:11+00:00Added an answer on May 10, 2026 at 4:48 pm

    Just an opinion, but I would say the error text:

    • is part of the test, in which case getting it from the resource would be ‘wrong’ (otherwise you could end up with a consistantly mangled resource), so just update the test when you change the resource (or the test fails)
    • is not part of the test, and you should only care that it throws the exception.

    Note that the first option should let you test multiple languages, given the ability to run with a locale.

    As for multiple exceptions, I’m from C++ land, where creating loads and loads of exceptions (to the point of one per ‘throw’ statement!) in big heirachies is acceptable (if not common), but .Net’s metadata system probably doesn’t like that, hence that advice.

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

Sidebar

Ask A Question

Stats

  • Questions 84k
  • Answers 84k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you talking about the formatted page that .NET shows… May 11, 2026 at 4:58 pm
  • Editorial Team
    Editorial Team added an answer You cannot have more than one thread which interacts with… May 11, 2026 at 4:58 pm
  • Editorial Team
    Editorial Team added an answer timeit from the Windows Server 2003 Resource Kit should do… May 11, 2026 at 4:58 pm

Related Questions

When I add a breakpoint and hit F5 to run in the debugger (I
I am looking for the best way to test if a website is alive
I've been trying to understand Process.MainWindowHandle . According to MSDN; The main window is
This is driving me nuts. I am using some 3rd-party code in a Windows

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.