Announcement

Collapse
No announcement yet.

Try and Catch in VB .Net

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Try and Catch in VB .Net

    Hi all

    I'm trying to create a little program that will automate some of our helpdesk tasks and i want to make it as bullit proof as possible. So for one of the first times im trying to using the try and catch for error checking. Here is what i have creating a folder:

    Code:
                Try
                    Directory.CreateDirectory("c:\testing")
                Catch ex As Exception
                    MsgBox("Directory already exists")
                End Try
    But i don't get an error when the folder already exisits. I thought it might be just overwriting the folder so i denied access to the folder and no error message. Please help! What have i done wrong?

    Thanks in advance
    Server 2000 MCP
    Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

  • #2
    Re: Try and Catch in VB .Net

    Checking the MS documentation here seems to suggest that there is no exception for an existing directory but there is syntax for checking if it exists first


    Tom
    Tom Jones
    MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
    PhD, MSc, FIAP, MIITT
    IT Trainer / Consultant
    Ossian Ltd
    Scotland

    ** Remember to give credit where credit is due and leave reputation points where appropriate **

    Comment


    • #3
      Re: Try and Catch in VB .Net

      What about if the user doesn't have the right to create the folder maybe? of the location is incorrect like a mapped drive that hasn't been mapped yet?

      Thanks
      Server 2000 MCP
      Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

      ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

      Comment


      • #4
        Re: Try and Catch in VB .Net

        Originally posted by tonyyeb
        What about if the user doesn't have the right to create the folder maybe? of the location is incorrect like a mapped drive that hasn't been mapped yet?

        Thanks
        The method you're using is a function which will return the path if it succeeds or a series of exceptions if it does not...

        Originally posted by MSDN

        Exceptions

        Exception type Condition
        IOException
        The directory specified by path is read-only or is not empty.

        UnauthorizedAccessException
        The caller does not have the required permission.

        ArgumentException
        path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

        ArgumentNullException
        path is a null reference (Nothing in Visual Basic).

        PathTooLongException
        The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.

        DirectoryNotFoundException
        The specified path is invalid (for example, it is on an unmapped drive).

        NotSupportedException
        An attempt was made to create a directory with only the colon character (.
        You can use try, catch, except to inform you of the type of error.

        Code:
        Try
        
          Do something...
        
        Catch ex as Exception
        
          MsgBox.show("An error has occurred:" & " " & ex.tostring)
        
        End Try
        When using try, catch, except you're probably not going to use a customized error message unless you know exactly what is occurring and want to handle it silently. Otherwise you should cast the exception as text and include it in your show message or console writeline.
        Last edited by ahinson; 30th June 2006, 22:11.
        Andrew

        ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

        Comment


        • #5
          Re: Try and Catch in VB .Net

          Well i finally got it working. Thanks guys. I would like to get more descriptive error messages but at the end of the day it is an internal app so it isn't a huge problem.
          Server 2000 MCP
          Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

          ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

          Comment

          Working...
          X