继续“抛出新例外”之后继续

时间:2017-11-19 15:20:04

标签: c# 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);
}

4 个答案:

答案 0 :(得分:2)

抓住异常with a try catch并处理它。

答案 1 :(得分:1)

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

更多信息here

那应该是什么:

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
        }

    }

OR:

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

使用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{ }