未处理的异常,无法调试

时间:2012-03-06 20:20:21

标签: c# .net console-application visual-studio-debugging unhandled-exception

我正在尝试调试检查MIPS语法的c#应用程序。但它不允许调试它。无论我在哪里输入我的断点,它都会被忽略,包括Main()函数的第一行。它也给我这个错误。 如果我不调用HasValidParams(),'add a b c'工作正常 'add a b'会在同样的情况下抛出异常 调用HasValidParams()

时都不起作用

enter image description here

Program.cs的

private static void Main(string[] args)
        {
            var validator = new MipsValidator();
            Console.Write("Please enter a MIPS statement: ");
            string input = Console.ReadLine();
            List<string> arguments = input.Split(new char[0]).ToList();
            Response status = validator.IsSyntaxValid(arguments);
            //Check syntax
            if (status.Success.Equals(true))
            {
                Response stat = validator.HasValidParams(arguments);
                //Check parameters
                if (stat.Success.Equals(true))
                {
                Console.WriteLine(string.Format("'{0}' is a valid mips instruction ", input));
                }
                else
                {
                    foreach (var reason in stat.Reasons)
                    {
                        Console.WriteLine(reason);
                    }
                }
            }
            else
            {
                foreach (string reason in status.Reasons)
                {
                    Console.WriteLine(reason);
                }
            }
        }

MIPS-validator.cs

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace mips_validator.utils
{
    public class MipsValidator : IMipsValidator
    {
        #region Implementation of IMipsValidator

        public Response IsSyntaxValid(List<string> args)
        {
            var response = new Response {Success = true};
            var op = (Operator) Enum.Parse(typeof (Operator), args[0]);
            switch (op)
            {
                case Operator.addi:
                case Operator.add:
                case Operator.beq:
                    if (args.Count != 4)
                    {
                        response.Reasons.Add(string.Format("4 operands required for {0}, {1} parameters provided.",
                                                           op, args.Count));
                        response.Success = false;
                    }
                    break;
                case Operator.j:
                    if (args.Count != 2)
                    {
                        response.Reasons.Add(string.Format("1 operands required for {1}, {0} parameters provided.",
                                                           args.Count, op));
                        response.Success = false;
                    }
                    break;
                default:
                    response.Reasons.Add(string.Format("{0} is an unknown mips operation", op));
                    response.Success = false;
                    break;
            }
            return response;
        }

        public Response HasValidParams(List<string> parameters)
        {
            string op1, op2, op3;
            var temporary = new Regex(@"/\$t\d+/");
            var store = new Regex(@"/\$s\d+/");
            var zero = new Regex(@"/\$zero/");
            var osReserved = new Regex(@"/\$k0|1/");
            var memory = new Regex(@"");
            var constant = new Regex(@"/-?\d*/");
            var label = new Regex(@"/.*\:/");
            Operator operation;
            var response = new Response {Success = true};
            string opString = parameters[0];
            Enum.TryParse(opString.Replace("$", string.Empty), true, out operation);
            switch (operation)
            {
                case Operator.add:
                    {
                        op1 = parameters[1];
                        op2 = parameters[2];
                        if (!temporary.IsMatch(op1) && !store.IsMatch(op1) && !zero.IsMatch(op1))
                        {
                            response.Reasons.Add(string.Format("{0}: error register expected", op1));
                            response.Success = false;
                        }
                        if (!temporary.IsMatch(op2) && !store.IsMatch(op2) && !zero.IsMatch(op2))
                        {
                            response.Reasons.Add(string.Format("{0}: error register expected", op2));
                            response.Success = false;
                        }
                    }
                    break;
                case Operator.addi:
                    {
                        op1 = parameters[1];
                        op2 = parameters[2];
                        if (!temporary.IsMatch(op1) && !store.IsMatch(op1) && !zero.IsMatch(op1))
                        {
                            response.Reasons.Add(string.Format("{0}: error register expected", op1));
                            response.Success = false;
                        }
                        if (!constant.IsMatch(op2) && !zero.IsMatch(op2))
                        {
                            response.Reasons.Add(string.Format("{0}: error constant expected", op2));
                            response.Success = false;
                        }
                    }
                    break;
                case Operator.beq:
                    {
                        op1 = parameters[1];
                        op2 = parameters[2];
                        op3 = parameters[3];
                        if (!temporary.IsMatch(op1) && !store.IsMatch(op1) && !zero.IsMatch(op1))
                        {
                            response.Reasons.Add(string.Format("{0}:  error register expected", op1));
                            response.Success = false;
                        }
                        if (!temporary.IsMatch(op2) && !store.IsMatch(op2) && !zero.IsMatch(op2))
                        {
                            response.Reasons.Add(string.Format("{0}: error register expected", op2));
                            response.Success = false;
                        }
                        if (!label.IsMatch(op3) && !constant.IsMatch(op3))
                        {
                            response.Reasons.Add(string.Format("{0}: error label or constant expected", op3));
                            response.Success = false;
                        }
                    }
                    break;
            }

            return response;
        }

        #endregion
    }
}

解------- Response.cs(旧)

public class Response
{
    public List<string> Reasons;
    public bool Success = true;

}

Response.cs(电流)

 public class Response
    {
        public Response()
        {
            Reasons = new List<string>();
            Success = true;
        }
        public List<string> Reasons;
        public bool Success = true;

    }

3 个答案:

答案 0 :(得分:3)

我无法判断您是否正在寻找能够调试项目的方法,或者您是否希望在代码中被告知潜在问题。

对于后者:

确保Response.Reasons(或字段初始值设定项)的构造函数初始化Response

答案 1 :(得分:2)

您没有显示Response类,因此请确保Reasons实际设置为您可以添加到的集合,而不是保留为默认值null。

编辑:@nodakai指出以下可能导致崩溃的原因不是一个人;结果是一个空的char数组是一个在空格上拆分的特殊情况。

*你通过做计算参数     List arguments = input.Split(new char [0])。ToList(); ...除了把原始字符串放在List中之外,据我所知,绝对没有。您可能希望拆分new char[] {' '}而不是拆分空格。*

答案 2 :(得分:1)

检查断点是否如下所示:

enter image description here

如果是,则源代码与程序集实际编译的代码不同。确保您的项目构建正确(右键单击解决方案并选择“Rebuild”)并检查您当前的配置:

enter image description here

希望这会有所帮助......