Results 1 to 6 of 6

Thread: [RESOLVED] [2005] Error catching

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    the UK
    Posts
    265

    Resolved [RESOLVED] [2005] Error catching

    I'm trying to improve my app with some error catching.

    I have a module that downloads a file from the Internet to a local folder. At the moment, if the local folder doesn't exist, it just crashes here:

    Code:
    strLocalFile = New FileStream(pPath, FileMode.Create, FileAccess.Write, FileShare.None)
    with error:

    System.IO.DirectoryNotFoundException was unhandled
    Message="Could not find a part of the path 'C:\Documents and Settings\Simon\Desktop\test\download.zip'."
    Source="mscorlib"
    How can I do something with that error and stop my app from crashing?

    Ideally, have a messagebox show saying 'directory not found' or similar.

    Thanks

    Simon

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Error catching

    Maybe something like this:

    Code:
            Try
                'CHECK FOR AND CREATE DIRECTORY FIRST
                Dim DirectoryName As String = Path.GetDirectoryName(pPath)
                If Not Directory.Exists(DirectoryName) Then
                    Directory.CreateDirectory(DirectoryName)
                End If
    
                strLocalFile = New FileStream(pPath, FileMode.Create, FileAccess.Write, FileShare.None)
                '
                '
                '
            Catch ex As Exception
                'HANDLE ERROR HERE
                MessageBox.Show(ex.Message)
            End Try

  3. #3
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Error catching

    you can have Try...Chatch block to catch the excception thrown and sho a messegbox telling the user about the problem.
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                '...
                '...
                strLocalFile = New FileStream(pPath, FileMode.Create, FileAccess.Write, FileShare.None)
            Catch ex As DirectoryNotFoundException
                MessageBox.Show(ex.Message)
                'you can chach some other exceptions as well.
            Catch ex As FileNotFoundException
                '...
            End Try
        End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    the UK
    Posts
    265

    Re: [2005] Error catching

    Thanks guys,

    I'm getting an error at:

    Code:
    If Not Directory.Exists(DirectoryName) Then
                    Directory.CreateDirectory(DirectoryName)
                End If
    Saying that Name 'Directory' is not declared.

    What am I missing?

    Cheers

    Simon

  5. #5
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Error catching

    I think you are missing the IO namespace. Try
    Code:
    IO.Directory

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    the UK
    Posts
    265

    Re: [2005] Error catching

    Thanks

    Code:
    If Not IO.Directory.Exists(DirectoryName) Then
                    IO.Directory.CreateDirectory(DirectoryName)
                End If
    Works great.

    Simon

Posting Permissions

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



Click Here to Expand Forum to Full Width