我正在编写一个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个值都在字典中。有人可以告诉我为什么会抛出这个错误吗?是否有建议的方法来确保处理异常?如果是这样,你推荐什么?
答案 0 :(得分:2)
更改throw new NotImplementedException("Category not found");
到
throw new NotImplementedException(String.Concat"Category not found :",row[sicIndex]));
当你意识到你想要像
这样的东西时,你会发自己几次switch(row[sicIndex].Category)
别担心我们都做到了。