0

I have a class Dispatcher with a method Send as follows:

public class Dispatcher : IDispatcher {

  public void Send(Order order) {
    Type type = typeof(IOrderHandler<>).MakeGenericType(order.GetType());
    IOrderHandler handler = (IOrderHandler)ObjectFactory.GetInstance(type);

    try {
      handler.Handle(order);
    } catch (Exception exception) {
      ILogger logger = ObjectFactory.GetInstance<ILogger>();
      logger.Send(exception);
    }

  }

} // Send

I am handling orders and catching exceptions ...

When I am debugging I would like to still fire the exception.

How can I do this?

Thank You, Miguel

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

4 Answers4

5

Just add this line to your catch block:

 if (System.Diagnostics.Debugger.IsAttached) throw;
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • +1, This is a much better approach in IDE, Good to know about `System.Diagnostics.Debugger.IsAttached` – Habib Oct 11 '13 at 18:05
  • I don't get it ... With this the exception will allways be fire in debug or release ... But when I have the site on the server what will happen? – Miguel Moura Oct 11 '13 at 18:06
  • @MDMoura, this only deals when you are debugging in Visual studio, when you have site on server, there wouldn't be any debugger attached. Unless you decide to [attach one ....](http://stackoverflow.com/questions/848987/attach-debugger-to-iis-instance) – Habib Oct 11 '13 at 18:07
  • Ah, got it! So your suggestion is great! I was trying [Conditional("DEBUG")] on an extra method which would fire the exception but I wasn't getting the point of origin. Thank you. – Miguel Moura Oct 11 '13 at 18:15
4

You can add the following in your catch block:

#if DEBUG
                throw;
#endif

So your code would look like:

            try
            {
                handler.Handle(order);
            }
            catch (Exception exception)
            {
                ILogger logger = ObjectFactory.GetInstance<ILogger>();
                logger.Send(exception);
#if DEBUG
                throw;
#endif
            }

If you want the exception notification in IDE during debugging in Release configuration, then use @Hans Passant's answer, because that will let you know about the exception for both Release and Debug configuration.

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
2

Well, based on the fact that you'd like the exception to still be thrown, but only when debugging, you could do this:

  1. Open the Debug menu and choose Exceptions.
  2. When the dialog loads, tick the check box next to Common Language Runtime Exceptions under the Thrown heading.

This will cause you to get a first chance exception. What that means is you'll be notified by the IDE, when debugging, and you'll get a chance to handle it before processing continues. This will actually let you see it before it even gets logged.

What's more, you can actually unselect exceptions you don't want with this approach because they are broken down by exception type underneath the Common Language Runtime Exceptions grouping.

More detail...

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
0

Go to Debug > Exception and check the CLR exceptions for "thrown", this will take you right there. alternatively place a breakpoint on the catch block.

Ahmed ilyas
  • 5,722
  • 8
  • 44
  • 72