0

I have a C# project. Is it possible to write code to the effect that "If an exception should occur while executing thus and such a task (and debugger is available), please break immediately, without unwinding the call stack."

Also, I just want to say, if this isn't possible, I'm fine with an answer to that effect.

William Jockusch
  • 26,513
  • 49
  • 182
  • 323
  • I don't understand, can't you add a `try catch` block and add a breakpoint in the `catch` block? – Maria Ines Parnisari May 07 '17 at 00:56
  • the call stack gets unwound – William Jockusch May 07 '17 at 00:59
  • There is not enough context in your question to understand exactly what you are trying to do and why you can't do it. If your code is throwing the exception, you can set a breakpoint at the `throw`. If some other code is throwing the exception, after you call it from your code, you can set the debugger settings to always break on that exception type instead of only when it's not handled. Please explain more precisely what it is you are trying to do and what _specifically_ you are having trouble figuring out. – Peter Duniho May 07 '17 at 01:13
  • Possible duplicate of [Exception thrown in debug mode, but where?](http://stackoverflow.com/a/15763898) – Peter Duniho May 07 '17 at 01:14
  • Possible duplicate of [Force break on any exception thrown in program](http://stackoverflow.com/q/1698154) – Peter Duniho May 07 '17 at 01:17
  • At the time I wrote this question, I didn't know where the exception was being thrown. It was one of those "once in a blue moon" exceptions, so setting a breakpoint for it was an annoying proposition. At any rate, I eventually got that answer. But more generally, I want to do exactly what I wrote. Seems like the answer to my question is "No, you can't do that." – William Jockusch May 07 '17 at 23:48

3 Answers3

0

You should take a look at the System.Diagnostics.Debugger class (https://msdn.microsoft.com/en-us/library/system.diagnostics.debugger(v=vs.110).aspx)

Using this class you can check to see if the debugger is attached and if it is you can break.

You could also wrap this in a static method on a utility class so you can use it easily

public static class DebuggerHelpers
{
    [Conditional("DEBUG")]
    public static void BreakIfDebugging()
    {
        if (System.Diagnostics.Debugger.IsAttached)
        {
             System.Diagnostics.Debugger.Break()
        }
    }
}

The Conditional attribute (https://msdn.microsoft.com/en-us/library/system.diagnostics.conditionalattribute(v=vs.110).aspx) will cause all calls to this method to be omitted when the DEBUG is not defined (AKA Release).

mageos
  • 1,216
  • 7
  • 15
  • yes, but it's not per-thread, and I have to leave the point in the call stack where the exception was thrown. – William Jockusch May 07 '17 at 04:22
  • Are you just wanting an easy way to ignore that a call to an external API throws an exception? – mageos May 07 '17 at 23:38
  • I don't think there is an easy way to do it. Your best bet would be some kind of IL weaver that would take your code and essentially wrap every statement in a try catch block and in the catch break if the debugger is attached to break. This would keep your code clean and add the behavior you are looking for. – mageos May 08 '17 at 02:45
0

If you want to leave the point where the exception was thrown and yet retain the call stack, you can do it only through logging the StackTrace AFAIK.

public static class Logger
{
  ...

  public static string CurrentStackDefaultLog()
  {
    // the true value is used to include source file info
    var l_CurrentStack = new System.Diagnostics.StackTrace(true);
    return l_CurrentStack.ToString();
  }

  ...
}

A good link for implementing this code is given in https://www.codeproject.com/Articles/223611/How-to-log-the-current-call-stack-in-NET by Daniele Mazzeranghi

vamsee
  • 31
  • 1
  • 9
0

Based on the other answers, it seems the answer to my question is that it can't be done.

William Jockusch
  • 26,513
  • 49
  • 182
  • 323