我有这个程序,我必须处理异常,但我从来没有这样做过,所以我有点困惑。我猜我必须处理异常,比如文本文件中的值是空的,没有索引,没有"="或者文件是空的,但我真的不知道如何定义它们或将它们放在哪里。这是我的代码:(文本文件中的行应该是这样的:EMPIS_MAG_BUDOW = 12
EMPIS_DESKA_FASOLKA = 2
SM_PORTAL_POL1 = 4)
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()
{
///////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 JOIN stanmag using(ID_KARTOTEKA);";
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]);
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]);
return new WynikPorownania {Indeks = index, Ilosc = (int)Math.Max(value, zwr(index))};
}).ToList();
//DISPLAY ALL LISTATF CLASSES
foreach(object WynikPorownania in listatf)
{
Console.WriteLine(WynikPorownania);
}
}
}
}
我试着做一个例外,如果值没有给出,但它不工作,程序仍然崩溃时,文本文件中的值为空
List<WynikPorownania> listatf = File.ReadAllLines(path).Select(line =>
{
var linia = line.Split("=");
string index = linia[0];
if (linia[1] == "")
{
throw new Exception("Is empty ... or whatever you caught");
return null;
}
else
{
int value = int.Parse(linia[1]);
}
return new WynikPorownania { Indeks = index, Ilosc = (int)Math.Max(value, zwr(index)) };
}).ToList();
要处理c#中的异常,你应该使用try catch(参见c#参考try-catch-finally)在你的代码中,我建议在火鸟连接周围包装一个try catch,因为你依赖于这个:
try {
// your code
} catch (Exception ex) {
// define what happens if exception is thrown
}
关键是你要在try中包含firebird连接和while循环,因为这些是你的代码所依赖的东西。
处理文本文件中的值:
//DISPLAY ALL LISTATF CLASSES
// note that I have replaced the "object" with your class.
foreach(WynikPorownania wynikPorownania in listatf)
{
if (wynikPorownania.Indeks != ... || wynikPorownania.Ilosc != ... )
throw new Exception("Is empty ... or whatever you caught");
Console.WriteLine(wynikPorownania);
}
//尝试如下。
class Program
{
public static void Main()
{
//CONNECTION
try
{
//... <YOUR CODE>
//DISPLAY ALL LISTATF CLASSES
foreach(object WynikPorownania in listatf)
{
Console.WriteLine(WynikPorownania);
/*if <your condition>
{
throw <your exception1>
}
else if <your condition>
{
throw <your exception1>
}*/
}
}
catch (Exception <your Exception>)
{
// Throw the message
// Or return the code
}
}
}