-4

我正在编写一个 C# 控制台应用程序,其目的是读取 CSV,为给定条目分配权重,以便权重可以在更大的地图应用程序中对纬度/经度坐标进行分类。读取 CSV 分组文件的应用程序部分如下:

var otherWeights = new Dictionary<string, Int32>();
var distressedWeights = new Dictionary<string, Int32>();
var industrialWeights = new Dictionary<string, Int32>();
var officeWeights = new Dictionary<string, Int32>();
var retailWeights = new Dictionary<string, Int32>();

//TODO: Get Listof SIC Codes and manually assign (a) a category (distressed, industrial, office or retail) and (b) a weight
otherWeights.Add("Other", 5);
distressedWeights.Add("Distressed", 4);
industrialWeights.Add("Industrial", 3);
officeWeights.Add("Office", 2);
retailWeights.Add("Retail", 1);
//These are used in assignments below
//TODO: get a unique list of all SIC present in this dataset.. probably about 1,000, and save as spread sheet

//establish connection to specified CSV.
string strCSVConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dataDirectory + ";"
+ "Extended Properties='text;HDR=YES;'";

using (var txt = new StreamReader(dataDirectory + "SICGroupingSummary.csv"))
using (var reader = new CsvReader(txt, true))
{
    var header = reader.GetFieldHeaders();
    var sicIndex = Array.IndexOf(header, "SIC_Code");
    var sicDesc = Array.IndexOf(header, "SIC_Description");
    var categoryIndex = Array.IndexOf(header, "Category");
    var weightIndex = Array.IndexOf(header, "Weight");

    foreach (string[] row in reader)
    {
        switch (row[sicIndex])
        {
            case "Distressed":
                distressedWeights[row[sicIndex]] = Int32.Parse(row[weightIndex]);
                break;
            case "Industrial":
                industrialWeights[row[sicIndex]] = Int32.Parse(row[weightIndex]);
                break;
            case "Office":
                officeWeights[row[sicIndex]] = Int32.Parse(row[weightIndex]);
                break;
            case "Retail":
                retailWeights[row[sicIndex]] = Int32.Parse(row[weightIndex]);
                break;
            case "Other":
                otherWeights[row[sicIndex]] = Int32.Parse(row[weightIndex]);
                break;
            default:
                throw new NotImplementedException("Category not found");
                break;
        }
    }
}

当我调试应用程序时,我收到一条错误消息:

A first chance exception of type 'System.NotImplementedException' occurred 

指向 switch 语句的最后一部分。

堆栈跟踪如下:

at BuildALU.Run() in c:\Users\Administrator\Documents\MapLarge\dataparse\DataParse  
\ActualLandUseMap\BuildALU.cs:line 79
at DataParse.Program.Main(String[] args) in c:\Users\Administrator\Documents\MapLarge
\dataparse\DataParse\Program.cs:line 14
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, 
String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, 
ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, 
ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,   
ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

我确保所有 5 个值都在字典中。有人可以告诉我为什么会抛出这个错误吗?是否有建议的方法来确保处理异常?如果是这样,你有什么建议?

4

1 回答 1

2

更改 throw new NotImplementedException("Category not found");

throw new NotImplementedException(String.Concat"Category not found :",row[sicIndex]));

然后当你意识到你想要类似的东西时打自己几次

switch(row[sicIndex].Category)

别担心,我们都做到了。

于 2013-07-09T16:30:26.773 回答