Why is .NET exception not caught by try/catch block?

前端 未结 25 727
Happy的楠姐
Happy的楠姐 2020-12-04 19:24

I\'m working on a project using the ANTLR parser library for C#. I\'ve built a grammar to parse some text and it works well. However, when the parser comes across an illeg

相关标签:
25条回答
  • 2020-12-04 19:56

    You can set up VS.Net to break as soon as any exception occurs. Just run your project in debug mode, and it will stop as soon as the exception is thrown. Then you should have a better idea of why it isn't being caught.

    Also, you can put some code in to catch all unhandled exceptions. Read the link for more info, but the basics are these two lines.

    Application.ThreadException += new ThreadExceptionEventHandler(ThreadExceptionHandler);
    
     // Catch all unhandled exceptions in all threads.
     AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler);
    
    0 讨论(0)
  • 2020-12-04 19:56

    I believe Steve Steiner is correct. When researching Steve's suggestions, I came across this thread talking about the "Enable Just My Code" option in Tools|Options|Debugger|General. It is suggested that the debugger will break in certain conditions when non-user code either throws or handles an exception. I'm not exactly sure why this even matters, or why the debugger specifically says the exception was unhandled when it really was.

    I was able to eliminate the false breaks by disabling the "Enable Just My Code" option. This also changes the Debug|Exceptions dialog by removing the "User-handled" column as it no longer applies. Or, you can just uncheck the "User-handled" box for CLR and get the same result.

    Bigtime thanks for the help everyone!

    0 讨论(0)
  • 2020-12-04 19:57

    To me, a catch (Exception) clause should've captured any exception whatsoever. Is there any reason why it wouldn't?

    The only possibility I can think of is that something else is catching it before you and handling it in a way that appears to be an uncaught exception (e.g. exiting the process).

    my try/catch block won't catch it and instead stops execution as an unhandled exception.

    You need to find what is causing the exit process. It might be something other than an unhandled exception. You might try using the native debugger with a breakpoint set on "{,,kernel32.dll}ExitProcess". Then use SOS to determine what managed code is calling exit process.

    0 讨论(0)
  • 2020-12-04 19:58

    @spoulson,

    If you can replicate it, can you post it somewhere? One avenue you could try is usign WinDBG with the SOS extensions to run the app and catch the unhandled exception. It will break on the first chance exception (before the runtime tries to find a handler) and you can see at that point where it is coming from, and what thread.

    If you haven't used WinDBG before, it can be a little overwhelming, but here's a good tutorial:

    http://blogs.msdn.com/johan/archive/2007/11/13/getting-started-with-windbg-part-i.aspx

    Once you start up WinDBG, you can toggle the breaking of unhandled exceptions by going to Debug->Event Filters.

    0 讨论(0)
  • 2020-12-04 19:58

    Wow, so of the reports so far, 2 worked correctly, and 1 experienced the issue I reported. What are the versions of Windows, Visual Studio used and .NET framework with build numbers?

    I'm running XP SP2, VS 2008 Team Suite (9.0.30729.1 SP), C# 2008 (91899-270-92311015-60837), and .NET 3.5 SP1.

    0 讨论(0)
  • 2020-12-04 20:03

    The best option sounds like setting Visual Studio to break on all unhandled exceptions (Debug -> Exceptions dialog, check the box for "Common Language Runtime Exceptions" and possibly the others as well). Then run your program in debug mode. When the ANTLR parser code throws an exception it should be caught by Visual Studio and allow you to see where it is occurring, the exception type, etc.

    Based on the description, the catch block appears to be correct, so one of several things could be happening:

    1. the parser is not actually throwing an exception
    2. the parser is ultimately throwing something that isn't deriving from System.Exception
    3. there is an exception being thrown on another thread that isn't being handled

    It sounds like you have potentially ruled out issue #3.

    0 讨论(0)
提交回复
热议问题