try
{
if (!File.Exists("File.Ext"))
throw new FileNotFoundException();
}
catch(FileNotFoundException e)
{
// your message here.
}
在写这篇文章时,我发现了上面的代码,认为必须有一种方法在一个块中完成它我试图检查读取 bmp 的错误
Bitmap b2;
b2 = new Bitmap("g:\\btmp1.bmp");
因此,如果将文件分配给“b2”时出现问题,则会出现错误
string notExst = "";
Bitmap b2;
try
{
b2 = new Bitmap("g:\\ba.bmp");
}
catch (FileNotFoundException e)
{
throw e.Tostring(); \\ would it be ok to
\\ msgbox.show(e.Tostring()) insted of throw ?
}
我想我在 Try Catch 中犯了语法错误,正确的方法是什么?谢谢。
在编辑乔伊之后,我不明白你的回答我想被告知并相应地打破方法而不是粉碎
最后
public bool TryGetBitMap(string FilePath)
{
string NotExstMsg = "The file: " + FilePath + "Could Not Be Found!";
bool exst = false;
if (!File.Exists(FilePath))
MessageBox.Show(NotExstMsg);
else exst = true;
return exst;
}
private void ButScreenCupt_Click(object sender, EventArgs e)
{
string FilePath1 = "g:\\a.bmp";
string FilePath2 = "g:\\b.bmp";
Bitmap b1, b2;
bool isSuccess1 = TryGetBitMap(FilePath1);
bool isSuccess2 = TryGetBitMap(FilePath2);
if (isSuccess1 && isSuccess2)
{
b1 = new Bitmap(FilePath1);
pictureBox1.Image = b1;
b2 = new Bitmap(FilePath2);
pictureBox2.Image = b2;
dd = ComparingImages.Compare(b1, b2);
MessageBox.Show(dd.ToString());
}
}