代码之家  ›  专栏  ›  技术社区  ›  Lance Fisher

忽略C命令行程序中的try块

  •  3
  • Lance Fisher  · 技术社区  · 16 年前

    我有一个C语言的命令行程序,我用try-catch块包装了它,以防止它崩溃控制台。但是,当我调试它时,如果在DoStuff()方法的某个地方抛出异常,visualstudio将中断“catch”语句。我希望visualstudio中断异常发生的位置。最好的办法是什么?


    视觉上的场景?

    static void Main(string[] args)
    {
        try
        {
            DoStuff();
        }
        catch (Exception e)
        {  //right now I have a breakpoint here
            Console.WriteLine(e.Message);
        }
    }
    
    private void DoStuff()
    {
        //I'd like VS to break here if an exception is thrown here.
    }
    
    4 回复  |  直到 16 年前
        1
  •  8
  •   Paul Whitehurst    16 年前

    你可以打开 First chance exceptions 在VS中,这将允许您在出现异常时立即收到通知。

        2
  •  4
  •   tvanfosson    16 年前

    break on uncaught exceptions 在ifdefs中包装try/catch就是我要做的。

        3
  •  1
  •   cangerer    16 年前

    我相信在VS的早期版本中,有一个调试菜单项,其效果是“中断所有异常”。不幸的是,我手头没有以前的版本。

        4
  •  1
  •   Rinat Abdullin    16 年前

    下面是我如何为在continuous integration server上运行的控制台工具执行此操作:

    private static void Main(string[] args)
    {
      var parameters = CommandLineUtil.ParseCommandString(args);
    
    #if DEBUG
      RunInDebugMode(parameters);
    #else
      RunInReleaseMode(parameters);
    #endif
    }
    
    
    static void RunInDebugMode(IDictionary<string,string> args)
    {
      var counter = new ExceptionCounters();
      SetupDebugParameters(args);
      RunContainer(args, counter, ConsoleLog.Instance);
    }
    
    static void RunInReleaseMode(IDictionary<string,string> args)
    {
      var counter = new ExceptionCounters();
      try
      {
        RunContainer(args, counter, NullLog.Instance);
      }
      catch (Exception ex)
      {
        var exception = new InvalidOperationException("Unhandled exception", ex);
        counter.Add(exception);
        Environment.ExitCode = 1;
      }
      finally
      {
        SaveExceptionLog(parameters, counter);
      }
    }
    

    基本上,在发布模式下,我们捕获所有未处理的异常,将它们添加到全局异常计数器中,保存到某个文件中,然后返回错误代码退出。

    注:例外计数器、控制台等来自 Lokad Shared Libraries