我得到一个递归调用一个抛出堆栈溢出异常的方法.第一个调用被try catch块包围,但没有捕获异常.
堆栈溢出异常是否以特殊方式运行?我可以正确捕获/处理异常吗?
注意:如果相关:
主线程中没有抛出异常
代码抛出异常的对象由Assembly.LoadFrom(...)手动加载.CreateInstance(...)
我正在使用针对C#的ANTLR解析器库开发一个项目.我已经构建了一个语法来解析一些文本,它运行良好.但是,当解析器遇到非法或意外的令牌时,它会抛出许多异常中的一个.问题是在某些情况下(并非所有)我的try/catch块不会捕获它而是将执行作为未处理的异常停止.
问题在于我无法在其他任何地方复制此问题,而是在我的完整代码中.调用堆栈显示异常肯定发生在我的try/catch(Exception)块中.我唯一能想到的是,在我的代码和引发异常的代码之间发生了一些ANTLR程序集调用,而且这个库没有启用调试,所以我无法单步执行它.我想知道不可调试的程序集是否会抑制异常冒泡?调用堆栈看起来像这样; 外部程序集调用在Antlr.Runtime中:
Expl.Itinerary.dll!TimeDefLexer.mTokens() Line 1213 C#
Antlr3.Runtime.dll!Antlr.Runtime.Lexer.NextToken() + 0xfc bytes
Antlr3.Runtime.dll!Antlr.Runtime.CommonTokenStream.FillBuffer() + 0x22c bytes
Antlr3.Runtime.dll!Antlr.Runtime.CommonTokenStream.LT(int k = 1) + 0x68 bytes
Expl.Itinerary.dll!TimeDefParser.prog() Line 109 + 0x17 bytes C#
Expl.Itinerary.dll!Expl.Itinerary.TDLParser.Parse(string Text = "", Expl.Itinerary.IItinerary Itinerary = {Expl.Itinerary.MemoryItinerary}) Line 17 + 0xa bytes C#
Parse()中最底部调用的代码片段如下所示:
try {
// Execution stopped at parser.prog()
TimeDefParser.prog_return prog_ret = parser.prog();
return prog_ret == null ? null : prog_ret.value;
}
catch (Exception ex) {
throw new ParserException(ex.Message, ex);
}
Run Code Online (Sandbox Code Playgroud)
对我来说,一个catch(Exception)子句应该捕获任何异常.有什么理由不这样做吗?
更新:我使用Reflector跟踪外部组件,没有发现任何线程的证据.该程序集似乎只是ANTLR生成的代码的运行时实用程序类.引发的异常来自TimeDefLexer.mTokens()方法,其类型为NoViableAltException,它派生自RecognitionException - > …
据我所知,Visual Studio(2008年和2010年)曾经有一个选项可以在抛出的异常或未处理的异常上中断.现在,当我打开Exceptions对话框(Ctr + Alt + E)时,它只是在抛出异常时提供中断:

我已经尝试调整该对话框中的列,但这没有帮助.这是一个错误,还是我错过了什么?
debugging exception visual-studio-2010 visual-studio-2008 visual-studio
考虑以下"安全"计划:
internal class Safe
{
public static void SafeMethodWillNeverThrow()
{
try
{
var something = ThrowsNewException();
Func<int, string> x = p => something.ToString();
}
catch (Exception)
{
}
}
private static object ThrowsNewException()
{
throw new Exception();
}
public static void Main()
{
SafeMethodWillNeverThrow();
}
}
Run Code Online (Sandbox Code Playgroud)
它永远不会以异常完成.但是为什么它在我运行时失败了?为什么SafeMethodWillNeverThrow()会抛出异常?
在测试此代码之前,请阅读以下答案.
我正在尝试运行以下代码:
class Program
{
static void Main(string[] args)
{
var task = Task.Factory.StartNew(() =>
{
throw new ApplicationException("message");
});
try
{
task.ContinueWith(t => Console.WriteLine("End"));
}
catch (AggregateException aex)
{
Console.Write(aex.InnerException.Message);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我预计Exception会被捕获在以下位置:
catch (AggregateException aex)
{
Console.Write(aex.InnerException.Message);
}
Run Code Online (Sandbox Code Playgroud)
但这不会发生.为什么会这样?
我有一个try,catch和finally块的函数.如果捕获到异常,那么我会捕获该异常的某些参数,例如其错误代码,错误详细消息和消息,并将其打印在excel文件中.我发布以下相关代码:
public void UpdateGroup(String strSiteID, String strGroup, int row)
{
try
{
Console.WriteLine("UpdateGroup");
Excel1.MWMClient.MWMServiceProxy.Group group = new Excel1.MWMClient.MWMServiceProxy.Group();
group.name = "plumber";
group.description = "he is a plumber";
Console.WriteLine(groupClient.UpdateGroup(strSiteID,group));
ExcelRecorder(0, null, null, row);
}
catch (System.ServiceModel.FaultException<DefaultFaultContract> ex)
{
ExcelRecorder(ex.Detail.ErrorCode, ex.Detail.Message, ex.Message, row);
}
finally
{
System.GC.Collect();
}
}
public void ExcelRecorder(int error, string detailmessage, string message, int row)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"D:/WebServiceTestCases_Output.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
if (!string.IsNullOrEmpty(message))
{
((Range)xlWorksheet.Cells[row, "M"]).Value2 …Run Code Online (Sandbox Code Playgroud)