"throw new Exception"后继续



当我抛出异常时,我的程序中断了。

我想在throw new MatchException后继续我的程序。

我的程序检查字符。当我输入错误的字符时,我得到异常并且我的程序中断。

我的代码:

public AbstractToken Peek()
{
    // Loop through all tokens and check if they match the input string
    foreach (KeyValuePair<Type, string> pair in _dictionary)
    {
        var match = Regex.Match(_input.Substring(_counter), pair.Value);
        if (match.Success)
        {
            if (pair.Key.IsSubclassOf(typeof(AbstractToken)))
            {
                // Create new instance of the specified type with the found value as parameter
                var token = (AbstractToken)Activator.CreateInstance(pair.Key, new object[] { match.Value, _counter }, null);
                return token;
            }
        }
    }
    throw new MatchException(_input[_counter].ToString(CultureInfo.InvariantCulture), _counter);
}

通过尝试捕获捕获异常并处理它。

使用 trycatch 来处理异常。您现在正在做的是抛出一个 Exception ,这将导致程序崩溃。

更多信息在这里

那么它应该是什么:

public AbstractToken Peek()
    {
        // Loop through all tokens and check if they match the input string
        try
        {
            foreach (KeyValuePair<Type, string> pair in _dictionary)
            {
            var test = _input.Length;
            // TODO: See if substring does not impose a to harsh performance drop
            Match match = Regex.Match(_input.Substring(_counter), pair.Value);
            if (match.Success)
            {
                if (pair.Key.IsSubclassOf(typeof(AbstractToken)))
                {
                    // Create new instance of the specified type with the found value as parameter
                    AbstractToken token = (AbstractToken)Activator.CreateInstance(pair.Key, new object[] { match.Value, _counter }, null);
                    return token;
                }
            }
        }
        }
        catch(Exception e)
        {
            //Do whatever u want
        }
    }

或:

public AbstractToken Peek()
    {
        // Loop through all tokens and check if they match the input string
        foreach (KeyValuePair<Type, string> pair in _dictionary)
        {
            var test = _input.Length;
            // TODO: See if substring does not impose a to harsh performance drop
            Match match = Regex.Match(_input.Substring(_counter), pair.Value);
            if (match.Success)
            {
                if (pair.Key.IsSubclassOf(typeof(AbstractToken)))
                {
                    // Create new instance of the specified type with the found value as parameter
                    AbstractToken token = (AbstractToken)Activator.CreateInstance(pair.Key, new object[] { match.Value, _counter }, null);
                    return token;
                }
            }
        }
    }

这两个代码都不会触发任何Exception

使用trycatch块,并将catch块留空。

try
{
       foreach (KeyValuePair<Type, string> pair in _dictionary)
        {
            var test = _input.Length;
            // TODO: See if substring does not impose a to harsh performance drop
            Match match = Regex.Match(_input.Substring(_counter), pair.Value);
            if (match.Success)
            {
                if (pair.Key.IsSubclassOf(typeof(AbstractToken)))
                {
                    // Create new instance of the specified type with the found value as parameter
                    AbstractToken token = (AbstractToken)Activator.CreateInstance(pair.Key, new object[] { match.Value, _counter }, null);
                    return token;
                }
            }
        }
}
catch{ }

当您引发异常时,软件会在堆栈中查找一个 try-catch 块来处理它。如果未找到此块,程序将崩溃。

调用

堆栈保存您调用的所有函数,底部保存第一个函数,顶部保存当前函数。

最新更新