文本文件输入异常处理c#



我有一个程序,它从数据库中获取数据,将其放入列表中,然后从文本文件中读取数据,根据给定的索引比较两个值——一个来自文本文件,另一个来自数据库,并将较高的值返回给另一个列表。我的问题是我不能处理的问题,当一行文本文件缺少一个索引。当调用函数"zwr(index)"时,它给出了一个NullReferenceException,但我不知道如何处理它,在底部输入catch NullReferenceException不起作用,我真的不知道从现在开始要去哪里。我还需要让它在用户输入错误索引时不会崩溃。这是我的代码,希望有人能帮助:

using System;
using FirebirdSql.Data.FirebirdClient;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace dokselect
{
class indexstan
{
public string index;
public double standysp;
}
class WynikPorownania
{
public string Indeks;
public int Ilosc;
public override string ToString()
{
return Indeks + " : " + Ilosc;
}
}
class Program
{
public static void Main()
{
try
{
///////CONNECTION
string conn = "database=C:/PCBiznes/BAZA/IXION2_LOGMAG.FB;user=SYSDBA;password=masterkey;DataSource=192.168.24.112;Port=3050";
FbConnection myConnection = new FbConnection(conn);
FbDataReader myReader = null;
string sql = "select KARTOTEKA.indeks,STANMAG.standysp FROM kartoteka INNER JOIN stanmag using(ID_KARTOTEKA) WHERE stanmag.ID_MAGAZYN=10002;";
FbCommand myCommand = new FbCommand(sql, myConnection);
myConnection.Open();
myReader = myCommand.ExecuteReader();
///////LIST lista1
List<indexstan> listadb = new List<indexstan>();
double standysp;
string index;
while (myReader.Read())
{
index = myReader[0].ToString();
standysp = Convert.ToDouble(myReader[1]);
if(standysp<0)
{
standysp = 0;
}
listadb.Add(new indexstan { index=index, standysp=standysp });
//Console.WriteLine(myReader[0].ToString());
}
myConnection.Close();
Console.WriteLine(listadb.Count);

//RETURN STANDYSP FUNCTION
double zwr(string myIndex)
{
var result = listadb.FirstOrDefault(listadb => listadb.index == myIndex).standysp;
return result;
}
//zwr("EMPIS_DESKA_FASOLKA");
//READ FROM TXT AND RETURN HIGHER
string path = "C:/Users/Praktykant/Documents/textdocs/dok1.txt";

List<WynikPorownania> listatf = File.ReadAllLines(path).Select(line =>
{   
var linia = line.Split("=");
string index = linia[0];
int value = int.Parse(linia[1]);
if(value<0)
{
value = 0;
}
return new WynikPorownania { Indeks = index, Ilosc = (int)Math.Max(value, zwr(index)) };
}).ToList();
//DISPLAY ALL LISTATF CLASSES
foreach (WynikPorownania WynikPorownania in listatf)
{
Console.WriteLine(WynikPorownania);
}
}
catch (FileNotFoundException ex)
{
Console.WriteLine("Nie znaleziono pliku z podanej sciezki: "+ex);
}
catch (FormatException ex)
{
Console.WriteLine("Podaj indeksy i wartosci w formacie 'indeks=wartosc'. Blad zwrocil: "+ex);
}
catch (NullReferenceException ex)
{
Console.WriteLine("Nie podales prawidlowego indeksu. Blad zwrocil: "+ex);
return;
}
}
}
}

以这种方式处理异常:

double zwr(string myIndex)
{
var result = listadb.FirstOrDefault(listadb => listadb.index == myIndex)?.standysp;
return result ?? 0;
}

消除错误,但仍然将数据放在列表中,结果如下:

EMPIS_MAG_BUDOW: 12

: 8 <-这里是文本文件中没有给出索引的地方

SM_PORTAL_POL1: 0

如果没有给出索引,我需要它停止编译,因为这段代码以后会成为一个更大程序的一部分,这一段代码不能把整个程序折叠起来。我尝试在将数据返回到列表的函数中使用不同的if语句,并试图抛出异常,但似乎没有任何工作…

这样处理:

double zwr(string myIndex)
{
var result = listadb.FirstOrDefault(listadb => listadb.index == myIndex)?.standysp;
return result ?? 0;
}

您将得到异常,因为listadb.FirstOrDefault(listadb => listadb.index == myIndex)为空,而您正试图访问null.standysp;

所以使用?.,它将访问前一个语句只有当它不是null或返回null本身。

如果??不为空则返回左侧,如果左侧为空则返回右侧。