+ Post New Thread
Results 1 to 12 of 12
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. ...
  1. #1
    DAZZD88's Avatar
    Join Date
    Apr 2009
    Location
    Sunderland, Tyne and Wear
    Posts
    788
    Thank Post
    163
    Thanked 77 Times in 66 Posts
    Rep Power
    48

    Noob Coder (Visual C#) - Unhandled Exception

    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.
    Attached Images Attached Images

  2. #2

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,885
    Thank Post
    518
    Thanked 2,489 Times in 1,930 Posts
    Blog Entries
    24
    Rep Power
    839
    Also, when working with file systems, you should use a try { } catch() { } statement - to catch unhandled exceptions. eg. try-catch (C# Reference)

  3. Thanks to localzuk from:

    DAZZD88 (14th July 2010)

  4. #3
    DAZZD88's Avatar
    Join Date
    Apr 2009
    Location
    Sunderland, Tyne and Wear
    Posts
    788
    Thank Post
    163
    Thanked 77 Times in 66 Posts
    Rep Power
    48
    Quote Originally Posted by jinnantonnix View Post
    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.

  5. #4

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,885
    Thank Post
    518
    Thanked 2,489 Times in 1,930 Posts
    Blog Entries
    24
    Rep Power
    839
    Quote Originally Posted by DAZZD88 View Post
    You mean inside the brackets?

    @localzuk will do when I get a bit more practice under my belt. I know it's not to hard to do
    Yes, inside the brackets. 'txtTargetPath.Text' outputs an object, whereas 'txtTargetPath.Text.ToString()' outputs a string.

  6. #5

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,506
    Thank Post
    1,457
    Thanked 1,203 Times in 816 Posts
    Rep Power
    713
    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

    Directory Info @ MSDN

    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)

  7. Thanks to LosOjos from:

    DAZZD88 (14th July 2010)

  8. #6
    DAZZD88's Avatar
    Join Date
    Apr 2009
    Location
    Sunderland, Tyne and Wear
    Posts
    788
    Thank Post
    163
    Thanked 77 Times in 66 Posts
    Rep Power
    48
    Here's what I have so far. The exception is now handled but is it done correctly?

    Code:
            private void btnGo_Click(object sender, EventArgs e)
            {
                try
                {
                    if (txtTargetPath != null)
                    {
                        DirectoryInfo dirTarget = new DirectoryInfo(txtTargetPath.Text);
                        colFiles = dirTarget.GetFiles();
    
                        foreach (FileInfo filFiles in colFiles)
                        {
                            txtResults.AppendText(filFiles.Name + "\r\n");
                        }
                    }
                }
    
                catch (Exception objException)
                {
                    MessageBox.Show("An error occurred: " + objException.Message);
                }
            }

  9. #7
    sister_annex's Avatar
    Join Date
    Jan 2009
    Location
    Wolverhampton
    Posts
    594
    Thank Post
    100
    Thanked 136 Times in 120 Posts
    Rep Power
    49
    Quote Originally Posted by DAZZD88 View Post
    Here's what I have so far. The exception is now handled but is it done correctly?

    Code:
            private void btnGo_Click(object sender, EventArgs e)
            {
                try
                {
                    if (txtTargetPath != null)
                    {
                        DirectoryInfo dirTarget = new DirectoryInfo(txtTargetPath.Text);
                        colFiles = dirTarget.GetFiles();
    
                        foreach (FileInfo filFiles in colFiles)
                        {
                            txtResults.AppendText(filFiles.Name + "\r\n");
                        }
                    }
                }
    
                catch (Exception objException)
                {
                    MessageBox.Show("An error occurred: " + objException.Message);
                }
            }
    I think you still need to change where
    Code:
    (txtTargetPath != null)
    to
    Code:
    (txtTargetPath.text != null)
    and put an end if in

  10. #8

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,885
    Thank Post
    518
    Thanked 2,489 Times in 1,930 Posts
    Blog Entries
    24
    Rep Power
    839
    Quote Originally Posted by sister_annex View Post
    and put an end if in
    No such thing in c#. If is ended with a closing brace as he has done.

  11. #9
    sister_annex's Avatar
    Join Date
    Jan 2009
    Location
    Wolverhampton
    Posts
    594
    Thank Post
    100
    Thanked 136 Times in 120 Posts
    Rep Power
    49
    Oops so it is i work in vb.net so missed that one

  12. #10

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    Quote Originally Posted by DAZZD88 View Post
    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.

  13. Thanks to powdarrmonkey from:

    DAZZD88 (14th July 2010)

  14. #11
    DAZZD88's Avatar
    Join Date
    Apr 2009
    Location
    Sunderland, Tyne and Wear
    Posts
    788
    Thank Post
    163
    Thanked 77 Times in 66 Posts
    Rep Power
    48
    @powdarrmonkey I get what you mean, thanks for the advice.

  15. #12

    Join Date
    Mar 2010
    Posts
    4
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    If it still doesn't work, you might want to replace:

    Code:
    if (txtTargetPath != null)
    with

    Code:
    if (!String.IsNullOrEmpty(txtTargetPath.Text))

    You shouldn't have to use .ToString() because txtTargetPath.Text should already be a string.

SHARE:
+ Post New Thread

Similar Threads

  1. Need a helping hand from a VB6 coder please
    By SimpleSi in forum Coding
    Replies: 1
    Last Post: 2nd May 2010, 12:33 PM
  2. Replies: 2
    Last Post: 11th August 2009, 12:17 PM
  3. [Video] Coder Girl - great rap, clever...
    By mattx in forum Jokes/Interweb Things
    Replies: 4
    Last Post: 1st August 2009, 08:13 AM
  4. internet explorer exception list
    By jcs808 in forum Windows Server 2000/2003
    Replies: 0
    Last Post: 11th March 2009, 09:17 AM
  5. Replies: 1
    Last Post: 8th November 2007, 12:44 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •