try
{
if (!File.Exists("File.Ext"))
throw new FileNotFoundException();
}
catch(FileNotFoundException e)
{
// your message here.
}
while writing this post i found the code above , thinking there must be a way doing it in one block
i am trying to check for erros reading bmp with
Bitmap b2;
b2 = new Bitmap("g:\\btmp1.bmp");
so if theres a problem assigning the file to “b2” it will give an error
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 ?
}
i guess i made a syntax error in my Try Catch, what is the right way?
thanks.
After Edit
Joey, i dont understand your answer i want to be informed and break out of method accordingly
instead of crush
finally
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());
}
}
You could build your own method using a TryGet pattern.