title: Exceptions and testing - how to unit test something for specific exception when I catch them all?
body: <p>I'm writing MVC4 web application. Generally I try to put "try{}catch{}" block inside every controller method that returns ActionResult to the user. I do it in order to catch all Exceptions and display appropriate message, so user will never see something like:</p>
  
  <p><strong>"Reference not set to an instance of an object"</strong></p>
  
  <p>My controllers usually looks like this:</p>
  
  <pre><code>try
  {
  
  }
  catch(MyFirstCustomException ex)
  {
  //set some message for the user and do some cleaning etc.
  
  return ActionResult();
  }
  catch(MySecondCustomException ex) (and so on...)
  {
  //set some message for the user and do some cleaning etc.
  
  return ActionResult();
  }
  catch(Exception ex)
  {
  //set some message for the user and do some cleaning etc.
  
  return ActionResult();
  }
  </code></pre>
  
  <p>However now I got the following situation: I have <strong>AccountController</strong> and a <strong>LogIn</strong> method, I want to write a unit test (using Microsoft Unit Testing Framework),  that will assert that user which haven't activated his account, won't be able to log in. I have a special Exception named <strong>UserNotActivatedException</strong> that is thrown, when such attempt is detected. Problem is - since I catch all my exceptions within a controller, my test will never actually see this exception itself - thus the test will always fail. I managed to bypass the problem by creating special <strong>status enum</strong> for my model which looks like this:</p>
  
  <pre><code>public enum LoginViewModelStatus
  {
  NotLoggedIn = 0,
  LoginSuccessfull = 1,
  LoginFailed = 2,
  UserNotActivatedException = 3,
  UnknownErrorException = 100
  }
  </code></pre>
  
  <p>and by setting it to a certain value when something is happening (so when I catch my special UserNotActivatedException - I set <strong>loginModelStatus to UserNotActivatedException</strong> and so on)</p>
  
  <p>My questions:</p>
  
  <ol>
  <li>Are there any nicer alternatives to this? </li>
  <li>I'm thinking of using this design in other controllers as well, are there any downfalls here?</li>
  <li>Is it good design to use a lot of custom exceptions for displaying messages for users, or would it be better to use more mini if(someCondition){return false;} tests?</li>
  </ol>