2

I have a lot of throw new NotImplementedExceptions() throughout my whole application. For now I want to silence them and to show a custom Message Dialog instead.

For catching them I'm using:

AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) =>
{
     if(eventArgs.Exception is NotImplementedException) {
        return;
     }
}

But the problem is that the exception is still threw.

How can I silence the throw when I catch this type of Exception within this piece of code?

João Pereira
  • 673
  • 1
  • 9
  • 29
  • 1
    you can disable a specific exception during debug from exception window – Bizhan Jun 16 '20 at 18:15
  • Can't you just use a try catch, that catches NotImplemented Exceptions, and just ignore them? :) I mean it is dirty as hell, but I am assuming you are going to get rid of them eventually? – Morten Bork Jun 16 '20 at 19:31
  • The idea is not to silence the Exception during Debug. It's to silence all of them (for the time being) after the Release version is given to the clients. – João Pereira Jun 16 '20 at 21:12
  • And regarding the try/catch, well I have a looot of NotImplementedExceptions. I just wanted to check if there's was an easier and cleaner way to do this. – João Pereira Jun 16 '20 at 21:12

1 Answers1

1

It sounds like what you want to do is to do something nicer than exploding when a method you haven't implemented is invoked. I don't believe that is possible using AppDomain.FirstChanceException or the related UnhandledException. There's a good answer here that talks a bit about why simply suppressing exceptions is undesirable.

What you could do instead is use something besides raising an exception to mark methods as not implemented, like calling a helper that displays your message, when you haven't implemented something yet. You could use #if pragmas or the ConditionalAttribute to switch to actually throwing exceptions in non-DEBUG builds, if that's desirable. It's not that uncommon to use helpers for throwing exceptions anyway (see for example ThrowHelper in the BCL, or Throw in one of my own projects), as there are some benefits to avoiding throws.

This would look like:

public void UnImplementedMethod()
{
  // rather than "throw new NotImplementedException("some message")"
  MyHelper.NotImplemented("some message");
}

// .... 

static class MyHelper
{
  [Conditional("DEBUG")]  
  public static void NotImplemented(string msg)
  {
#if DEBUG // can use whatever configuration parameter
      MessageBox.Show("Not Implemented: "+ msg);
#else
      throw new NotImplementedException(msg);
#endif
  }
}

You can use generic parameters to handle unimplemented methods that have non-void returns, though you have to decide what to actually return if you don't throw an exception. With this pattern you can do whatever you'd like, and still easily find places that haven't been implemented.

Kevin Montrose
  • 22,191
  • 9
  • 88
  • 137
  • 2
    Thanks for answering. I also thought about that option but since I have to make a lot of changes, I wanted to avoid it and check if it was possible with AppDomain instead. But yeah, if in the end what I want isn't possible, I will gladly mark your answer as the solution, since it's a correct and detailed answer :) – João Pereira Jun 16 '20 at 21:10