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

前端 未结 25 729
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:50

    I downloaded the sample VS2008 project, and am a bit stumped here too. I was able to get past the exceptions however, although probably not in a way that will work will great for you. But here's what I found:

    This mailing list post had a discussion of what looks to be the same issue you are experiencing.

    From there, I added a couple dummy classes in the main program.cs file:

    class MyNoViableAltException : Exception
    {
        public MyNoViableAltException()
        {
        }
        public MyNoViableAltException(string grammarDecisionDescription, int decisionNumber, int stateNumber, Antlr.Runtime.IIntStream input)
        {
        }
    }
    class MyEarlyExitException : Exception
    {
        public MyEarlyExitException()
        {
        }
    
        public MyEarlyExitException(int decisionNumber, Antlr.Runtime.IIntStream input)
        {
        }
    }
    

    and then added the using lines into TimeDefParser.cs and TimeDefLexer.cs:

    using NoViableAltException = MyNoViableAltException;
    using EarlyExitException = NoViableAltException; 
    

    With that the exceptions would bubble into the fake exception classes and could be handled there, but there was still an exception being thrown in the mTokens method in TimeDefLexer.cs. Wrapping that in a try catch in that class caught the exception:

                try
                {
                    alt4 = dfa4.Predict(input);
                }
                catch
                {
                }
    

    I really don't get why wrapping it in the internal method rather than where it is being called from handle the error if threading isn't in play, but anyways hopefully that will point someone smarter than me here in the right direction.

提交回复
热议问题