Coding Thread, Noob Coder (Visual C#) - Unhandled Exception in Coding and Web Development; Right people, I have recently attempted learning Visual C# with the help of a Sam's Learn in 24 Hours Book. ...
Right people, I have recently attempted learning Visual C# with the help of a Sam's Learn in 24 Hours Book. I felt like I had learned just enough to muddle through creating my own simple program that, at the moment, simply lists files in a chosen directory.
However, it spits out an exception when the target path is empty, which I was expecting it to do. Therefore I added an else statement (the relevant section of code is shown below) to display a messagebox informing the user of the problem.
Code:
private void btnGo_Click(object sender, EventArgs e)
{
if (txtTargetPath != null)
{
DirectoryInfo dirTarget = new DirectoryInfo(txtTargetPath.Text);
colFiles = dirTarget.GetFiles();
foreach (FileInfo filFiles in colFiles)
{
txtResults.AppendText(filFiles.Name + "\r\n");
}
}
else
MessageBox.Show("You have not selected a valid folder!");
}
What have I done wrong? The line in bold in the code is the line that VC# does not like.
DirectoryInfo needs a string, to try changing txtTargetPath.Text to txtTargetPath.Text.ToString
You mean inside the brackets? If you did, it didn't seem to work. I think the text in the textbox should be okay because it's set to the value of strTargetPath.
@localzuk will do when I get a bit more practice under my belt. I know it's not to hard to do
Last edited by DAZZD88; 14th July 2010 at 10:04 AM.
Just to add my two pence, if you search for methods on MSDN, it tells you which exceptions a method can throw and what they mean, couple that knowledge up with try-catch as localzuk said and you'll be well on your way. It's always a good idea to use try-catch when you're doing anything in your code that might throw an exception, otherwise when the program gets to the end user, rather than being able to carry on running or at least provide a description of the error, it'll just crash out to the error report screen
PS - in case you didn't know, when creating a new instance of a class, you're actually calling that classes constructor (thought that'd help you find the info you need on MSDN/Google)
Here's what I have so far. The exception is now handled but is it done correctly?
Never catch an exception you're not expecting to handle yourself. Always catch a specific exception and deal with it; if it's one you don't know how to handle, let it go up the chain to be dealt with properly by the operating system.
Assess the methods you are 'try'ing and what exceptions might be thrown by them, and catch those. Example: your call to DirectoryInfo.DirectoryInfo() can throw any of ArgumentNullException, SecurityException, ArgumentException, and PathTooLongException. Though in the latter case, you've read the documentation to find the upper limit and checked your argument before passing it to DirectoryInfo.DirectoryInfo(), right?
Do not just catch an exception of type Exception, except (heh) right at the top of your application where you are handling generic "something exceptional happened, deal with it gracefully and terminate" errors.