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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:02:38+00:00 2026-05-20T11:02:38+00:00

I am looking for a way to modify catch block depending on if it’s

  • 0

I am looking for a way to modify catch block depending on if it’s executed during the unit test run or not. The purpose is basically to detect/setup mock expectations which are swallowed because catch doesn’t rethrow.

I am using MSTest.

One obvious thing is using preprocessor but I don’t think it works. Especially if to use DEBUG define. There should be an easy way to detect that, shouldn’t it? I must have been looking for something wrong because I couldn’t find much info on that.

try {...}
catch(Exception)
{
    Log(...);
#if DEBUG
        throw;
#endif

}

ANSWER: extract the body of the try into another method and test that instead. Provided by Jon and Ritch. See discussion under Jon’s answer. Though thanks everyone for the contribution.

  • 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-05-20T11:02:39+00:00Added an answer on May 20, 2026 at 11:02 am

    How can you test the exception handling if you want to change the exception handling? Testing the altered behaviour doesn’t test what would happen if you didn’t alter the behaviour.

    Hopefully your mocks have other ways of allowing you to validate expectations at the end of the test, so you can avoid the exception being ignored that way – and you should be testing that your Log and Fix really does happen when you want it to. (Testing logging is tedious and rarely worth it, but presumably Fix does something important.) You should also be testing that the exception isn’t rethrown, if that’s part of the design.

    (As a side matter, catching ArgumentException is usually a bad sign to start with. But maybe in this case it’s due to a poorly-designed library you have to use.)

    EDIT: Just as a thought, if you’re going to be able to test Log and Fix, that means you have to be able to check whether or not they were invoked. In cases where you don’t expect an exception to be thrown, you should be able to verify that they weren’t called. If they were, you’ve effectively proved that an exception was thrown.

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

Sidebar

Related Questions

we are looking for a way to find out (not modify, just find out)
I'm looking for a way to modify my .hgignore file to ignore all Properties/AssemblyInfo.cs
I'm looking for the best way to modify the Boost Asio HTTP Server 3
I'm looking for way to PHP to detect if a script was run from
I'm looking for way to write Javascript programs / scripts on desktop, not inside
First, I'm not looking a way to parse an RSS-feed. I am creating an
I'm looking for a way to modify properties on a dynamic C# 4.0 object
I'm looking for a way to modify the i-th element of haskell List. let
I'm looking for a way to modify the values of I18n yml files for
I'm looking for a way to modify the html of a plugin template (e.g.

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.