locked
Catching exceptions RRS feed

  • Question

  • Hi

    For some reason the exception is caught sometimes it's not?

    Here is the code:

    Try        
    My.Computer.FileSystem.CopyFile(fullFilename, "Scripts" & "\" & ofd1.FileName,FileIO.UIOption.AllDialogs, FileIO.UICancelOption.ThrowException)
    Catch ex As System.OperationCancelledException
    MsgBox(ex.Message)
    End IF
    
    Thanks
    
    
    
    Sunday, August 29, 2010 10:27 PM

Answers

  • Hi,

    Try the code as.>>

     

    Try  
    My.Computer.FileSystem.CopyFile(fullFilename, "Scripts" & "\" & ofd1.FileName,FileIO.UIOption.AllDialogs, FileIO.UICancelOption.ThrowException)
    Catch ex As Exception
    MessageBox.Show(ex.ToString)
    End Try
    

     



    Regards,

    John


    • Proposed as answer by Frank L. Smith Monday, August 30, 2010 12:13 AM
    • Marked as answer by TonyFlora63 Tuesday, August 31, 2010 1:27 AM
    • Edited by John Anthony Oliver Tuesday, August 31, 2010 10:37 PM End If changed to "End Try" . Oops, sorry!!
    Sunday, August 29, 2010 11:24 PM

All replies

  • Hi,

    Try the code as.>>

     

    Try  
    My.Computer.FileSystem.CopyFile(fullFilename, "Scripts" & "\" & ofd1.FileName,FileIO.UIOption.AllDialogs, FileIO.UICancelOption.ThrowException)
    Catch ex As Exception
    MessageBox.Show(ex.ToString)
    End Try
    

     



    Regards,

    John


    • Proposed as answer by Frank L. Smith Monday, August 30, 2010 12:13 AM
    • Marked as answer by TonyFlora63 Tuesday, August 31, 2010 1:27 AM
    • Edited by John Anthony Oliver Tuesday, August 31, 2010 10:37 PM End If changed to "End Try" . Oops, sorry!!
    Sunday, August 29, 2010 11:24 PM
  • Tony,

    ... or make a combination of yours and John's with your exception first if you want to do something special with only that type exception.

    Do you see why John's will work and yours failed to though?

    That's critical to the solution (not only for this project, but future ones alike).

    Monday, August 30, 2010 12:13 AM
  • Hi John

    For some reason it's still hit or miss? This is strange?

    Thanks

    Monday, August 30, 2010 12:13 AM
  • Hi John

    For some reason it's still hit or miss? This is strange?

    Thanks


    What happened when it "missed" as you say? What are you trying to catch?
    Monday, August 30, 2010 12:14 AM
  • Hi Frank

    When I select no to overwrite a file soemtimes I still see the the progress bar as if the file was overwritten even though it was not.

    Monday, August 30, 2010 1:47 AM
  • Hi Tony,

     You have an end if at the last of your code could you post the complete If statement and the code for the progress bar. This might not be in the try catch but a code problem in the if statement or where the progress bar is coded.

    Curtis


    Always Lost in Code, Always mark answers as correct if they answer you question and solve your problem. This way others when searching for similar problems can find the answer faster.
    • Proposed as answer by Cor Ligthert Monday, August 30, 2010 6:52 AM
    Monday, August 30, 2010 5:05 AM
  • Sorry the End If should be an End Try
    Monday, August 30, 2010 12:52 PM
  • On Mon, 30 Aug 2010 14:52:58 +0200, <TonyFlora63> wrote:
     
    > Sorry the End If should be an End Try
     
    Hi,
    i just tested this piece of code and i can see this problem also. 
    Sometimes the error message is shown, if the file exists and the user 
    choose not to overwrite the file and sometimes just the "copyfile" dialog 
    appears.
    It also doesn`t matter if you catch the exception as exception ( which is 
    not the best way ) or as OperationCanceledException.
     

    Hannes

    If you have got questions about this, just ask.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Monday, August 30, 2010 1:15 PM
  • Maybe my problem is caused by using the the OFD box to browse for a file and save it to a hard coded path isntead of using the SFD box also.

    I've tried browsing with the OFD box and passing the file name to the SFD box to save a file to a hard coded path but can't seem to get it to work.

    Does anyone have any code that could accomplish this or maybe see where I'm going wrong with my code?

    The Code:

    Dim fullFilename As String
    
    Using ofd As New OpenFileDialog
      
      ofd.Title = "Select file to copy"
      ofd.Filter = "All files (*.*)|*.*"
      If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
        fullFilename = ofd.FileName
      Else
        
        Exit Sub
      End If
    
    End Using
    
    Using sfd As New SaveFileDialog
      
      Dim filename As String = IO.Path.GetFileName(fullFilename)
      sfd.FileName = filename
      sfd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
      sfd.Title = "Select save location"
      sfd.Filter = "All files (*.*)|*.*"
      If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
        Try
          My.Computer.FileSystem.CopyFile(fullFilename, sfd.FileName)
        Catch ex As Exception
          MessageBox.Show("Error copying file", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
      End If
    End Using 
    
    Monday, August 30, 2010 2:24 PM