我有這個程式,我必須處理例外,但我從來沒有做過,所以我有點困惑。我猜我必須處理例外,例如文本檔案中的值為空、沒有索引、沒有“=”或檔案為空,但我真的不知道如何定義它們或在哪里把他們。這是我的代碼:(文本檔案中的行應如下所示: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();
uj5u.com熱心網友回復:
要處理 C# 中的例外,您應該使用 try catch (請參閱c# refence try-catch-finally)在您的代碼中,我建議在 firebird 連接周圍包裹一個 try catch,因為您依賴于此:
try {
// your code
} catch (Exception ex) {
// define what happens if exception is thrown
}
例如,您將 firebird 連接和 while 回圈包含在 try 中的關鍵,因為這些是您的代碼所依賴的東西。
要處理文本檔案中的值:
//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);
}
uj5u.com熱心網友回復:
//嘗試像下面這樣的東西。
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
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/330928.html
