3

Visual Studio has this neat feature of popping up a message box whenever an exception is raised allowing you to break on it and inspect the program state even if the exception is theoretically caught later.

I would like to use something like this for logging purposes. Automatically log every exception that is being raised. This would be especially useful in cases where you raise an exception but it is caught in library code even though it should not have been.

Hence the question: Can it be done, if so, how?

Edit: Not sure if this was obvious enough: I do not want to write code to catch, log and rethrow all exceptions everywhere.

Sarien
  • 6,647
  • 6
  • 35
  • 55

1 Answers1

6

You cannot automatically log exceptions that are thrown. You can log exceptions that are created (regardless of whether they are thrown at a later stage) by means of writing your own exception class.

However, I rarely see someone creating an exception without actually throwing that exception; so this might be good enough for you.

Oswald
  • 31,254
  • 3
  • 43
  • 68
  • It's okay but would still require a huge amount of work to change all existing exceptions. Might work if you started working on a new project, though. – Sarien Oct 31 '13 at 12:00