Need help with handling exception
-
I have this assignment
m_strFileName = OpenFile();
in which there is a try/catch block within the OpenFile method.private string OpenFile() { string strFilename; if (intStoreNumber == 201) { strFilename = "t1"; } else if (intStoreNumber == 202) { strFilename = "t2"; } else if (intStoreNumber == 203) { strFilename = "T3"; } else { strFilename = intStoreNumber.ToString(); strFilename = strFilename.PadLeft(2,'0'); } strFilename = "afford" + strFilename + ".dat"; try { File.Copy("F:\\BORIS\\" + strFilename, Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + strFilename,true); } catch { MessageBox.Show("The file " + strFilename + " can not be found. Please check " + "that you selected the correct store and that your network connection to" + " drive F: is not disconnected","File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } strFilename = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + strFilename; return strFilename;
The question I have is if the program runs into this exception, how do I return it back to waiting on an event versus going through to the next method? Everything I got from google tells me all about the structure of a try/catch/finally block, but nothing much more on how to exit gracefully without shutting down the program. Thanx in advance.Jude
-
I have this assignment
m_strFileName = OpenFile();
in which there is a try/catch block within the OpenFile method.private string OpenFile() { string strFilename; if (intStoreNumber == 201) { strFilename = "t1"; } else if (intStoreNumber == 202) { strFilename = "t2"; } else if (intStoreNumber == 203) { strFilename = "T3"; } else { strFilename = intStoreNumber.ToString(); strFilename = strFilename.PadLeft(2,'0'); } strFilename = "afford" + strFilename + ".dat"; try { File.Copy("F:\\BORIS\\" + strFilename, Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + strFilename,true); } catch { MessageBox.Show("The file " + strFilename + " can not be found. Please check " + "that you selected the correct store and that your network connection to" + " drive F: is not disconnected","File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } strFilename = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + strFilename; return strFilename;
The question I have is if the program runs into this exception, how do I return it back to waiting on an event versus going through to the next method? Everything I got from google tells me all about the structure of a try/catch/finally block, but nothing much more on how to exit gracefully without shutting down the program. Thanx in advance.Jude
why would your program shut down? you have handled the exception and displayed a message box, the user will click the ok button on the message box and the program will continue further. Can you please elaborate your problem? what actually do u wanna do?
Thanks & Regards, Pramod "Everyone is a genius at least once a year"
-
why would your program shut down? you have handled the exception and displayed a message box, the user will click the ok button on the message box and the program will continue further. Can you please elaborate your problem? what actually do u wanna do?
Thanks & Regards, Pramod "Everyone is a genius at least once a year"
I do not want the program to shut down. The method that is called returns a value that must be used in the next step of the program. But since the exception was thrown, I do not want the program to progress to the next called method. I want it to kill the forward progress and have the app wait for an event. Thanx for the reply!
Jude
-
I do not want the program to shut down. The method that is called returns a value that must be used in the next step of the program. But since the exception was thrown, I do not want the program to progress to the next called method. I want it to kill the forward progress and have the app wait for an event. Thanx for the reply!
Jude
jsut make the following changes try { File.Copy("F:\\BORIS\\" + strFilename, Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + strFilename,true); strFilename = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + strFilename; } catch { MessageBox.Show("The file " + strFilename + " can not be found. Please check " + "that you selected the correct store and that your network connection to" + " drive F: is not disconnected","File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); strFilename = string.Empty; } return strFilename; and when u call the method OpenFile() just check if the return string is Empty or not if it is empty string dont do any thing else process furhter. Hope that is what you want to do...
Thanks & Regards, Pramod "Everyone is a genius at least once a year"
-
jsut make the following changes try { File.Copy("F:\\BORIS\\" + strFilename, Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + strFilename,true); strFilename = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + strFilename; } catch { MessageBox.Show("The file " + strFilename + " can not be found. Please check " + "that you selected the correct store and that your network connection to" + " drive F: is not disconnected","File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); strFilename = string.Empty; } return strFilename; and when u call the method OpenFile() just check if the return string is Empty or not if it is empty string dont do any thing else process furhter. Hope that is what you want to do...
Thanks & Regards, Pramod "Everyone is a genius at least once a year"
Thanx for the reply....but how do I stop the execution of the code? It is withing a button's click event. I would supply the code, but it is on my work desktop and I am at home. break would not work because it is not within a loop, etc
Jude
-
Thanx for the reply....but how do I stop the execution of the code? It is withing a button's click event. I would supply the code, but it is on my work desktop and I am at home. break would not work because it is not within a loop, etc
Jude
nevermind...brainfart!
Jude
-
Thanx for the reply....but how do I stop the execution of the code? It is withing a button's click event. I would supply the code, but it is on my work desktop and I am at home. break would not work because it is not within a loop, etc
Jude
what do u mean by stop the execution of the code. You just have to write the code in the IF block,,, if the string is not empty perform what ever you want else nothing... e.g. if(!String.Equals(String.Empty, Openfile()) { ///DO WHAT U WANT DO } else nothing will happen.... Is that fine?
Thanks & Regards, Pramod "Everyone is a genius at least once a year"
-
what do u mean by stop the execution of the code. You just have to write the code in the IF block,,, if the string is not empty perform what ever you want else nothing... e.g. if(!String.Equals(String.Empty, Openfile()) { ///DO WHAT U WANT DO } else nothing will happen.... Is that fine?
Thanks & Regards, Pramod "Everyone is a genius at least once a year"
yes, read previous post and forgive my brainfart!
Jude