guys, it's been a long while since the last I asked about this and so far, no one yet has ever given me the right and helpful answer.
My problem is, I have a program, I have published it, and I have been actually using it in my PC. Whenever it encounters an error, the JIT debugger comes out. How do I prevent it from popping up using try...catch blocks, or is there any other ways or code on how to do this?

please guys, I need an enlightenment on this.
Thanks

The main goal is to avoid the situations where an unexpected exception is thrown.

Using try / catch blocks can help you to properly catch the execptions, but not to avoid it.

Even more, you need to code the actions to do when the catch block is hit. IE: If you are doing actions against a database, you can catch the exception that the connection is not 'alive'. What to do then?.

As a suggestion, when you'll catch an unexpected exception, always write a record in a log with the relevant data to be analyzed later, and, if the execption is unbearable, then end the application and show the log.

Later, with the log info, try to code the application to avoid the exception.

Hope this helps

This is my code;

If ListBox1.Items.Count = 0 Or ListBox1.SelectedIndex = -1 Then
MsgBox("No item selected!", MsgBoxStyle.Exclamation, "Dean")
Else
FolderBrowserDialog1.ShowDialog()
Dim filetoMove As New IO.FileInfo(ToolStripStatusLabel3.Text & "\" & ListBox1.SelectedItem.ToString)
filetoMove.MoveTo(FolderBrowserDialog1.SelectedPath.ToString & "\" & filetoMove.Name) 'I think the error occured here
MsgBox("File: " & filetoMove.Name & "; moved to " & FolderBrowserDialog1.SelectedPath.ToString, MsgBoxStyle.OkOnly, "Dean")
ListBox1.Items.Remove(ListBox1.SelectedItem)
ToolStripStatusLabel1.Text = ""
ToolStripStatusLabel2.Text = ""
ToolStripStatusLabel4.Text = "[" & ListBox1.Items.Count & "]"
ToolStripStatusLabel5.Text = "Current Index: none"
End If

I am using this code to move a selected file from folder to another. The JIT error says, that moving is not granted because the program doesn't have access to the folder

Maybe you can try some thing like this untested code:

'
'   Verify if the source directory exists
'
Dim sourceDirectory as New IO.DirectoryInfo(ToolStripStatusLabel3.Text)
Try
    If sourceDirectory.Exists = False then
        MsgBox("The source directory '" & ToolStripStatusLabel3.Text & "' does not exists.")
        Exit Sub
    End If
Catch ex As Exception
    MsgBox("Got the following error acessing to '" & ToolStripStatusLabel3.Text & "'& ControlChars.CrLf & Ex.Message)
    Exit Sub
End Try
'
'  Verify if the source file exists
'
Dim filetoMove As New IO.FileInfo(ToolStripStatusLabel3.Text & "\" & ListBox1.SelectedItem.ToString)
Try
   If filetoMove.Exists = False then
        MsgBox("The source file'" & ToolStripStatusLabel3.Text & "\" & ListBox1.SelectedItem.ToString & "' does not exists.")
        Exit Sub
   End If
Catch ex1 As Exception
    MsgBox("Got the following error acessing to '" & ToolStripStatusLabel3.Text & "\" & ListBox1.SelectedItem.ToString & "'& ControlChars.CrLf & ex1.Message)
    Exit Sub
End Try
'
'   Verify if the destination directory exists. If not exists then Create it.
'
Dim destinationDirectory as New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
Try
    If destinationDirectory.Exists = False then
        If MsgBox("The destination directory '" & FolderBrowserDialog1.SelectedPath & "' does not exists. Do you want to create it?", MsgBoxStyle.YesNoCancel) <> MsgBoxReturn.Yes Then
           Exit Sub
        End If
        Try
            destinationDirectory.Create
        Catch ex2 As Exception
            MsgBox("Got the following error creating the directory '" & FolderBrowserDialog1.SelectedPath & "'" & ControlChars.CrLf & ex2.Message)
            Exit Sub
        End Try
    End If
Catch ex As Exception
    MsgBox("Got the following error acessing to '" & FolderBrowserDialog1.SelectedPath & "'& ControlChars.CrLf & ex.Message)
    Exit Sub
End Try
'
'   Verify if the destination file already exists. If exists ask for overwrite.
'
Dim destinationFile As New IO.FileINfo(FolderBrowserDialog1.SelectedPath.ToString & "\" & filetoMove.Name)
Try
   If destinationFile.Exists = True Then
      If MsgBox("The destination file '" & FolderBrowserDialog1.SelectedPath.ToString & "\" & filetoMove.Name & "' already exists. Do you want to overwrite it?", MsgBoxStyle.YesNoCancel) <> MsgBoxReturn.Yes Then
           Exit Sub
        End If
        Try
            destinationFile.Delete
        Catch ex2 As Exception
            MsgBox("Got the following error overwriting the file '" & FolderBrowserDialog1.SelectedPath & "\" & filetoMove.Name & "'" & ControlChars.CrLf & ex2.Message)
            Exit Sub
        End Try
    End If
Catch ex3 as Exception
    MsgBox("Got the following error overwriting the file '" & FolderBrowserDialog1.SelectedPath & "\" & filetoMove.Name & "'" & ControlChars.CrLf & ex3.Message)
    Exit Sub
End Try
'
'   Finally, move it :)
'
Try
    filetoMove.MoveTo(FolderBrowserDialog1.SelectedPath.ToString & "\" & filetoMove.Name) 'I think the error occured here
Catch ex As Exception
    MsgBox("Got the following error moving the file to '" & FolderBrowserDialog1.SelectedPath & "\" & filetoMove.Name & "'" & ControlChars.CrLf & ex3.Message)
    Exit Sub
End Try
MsgBox("File: " & filetoMove.Name & "; moved to " & FolderBrowserDialog1.SelectedPath.ToString, MsgBoxStyle.OkOnly, "Dean")

This is just a hint on how to. Here you can obtain the mesasage on the exception causing the problem.

If the destination directory exists then, maybe you have a permission problem.

To verofy if this is the case, see the properties of the destination directory, then verify you have the modify (write) permission in the destination folder, and verify that there is no quota limited space usage, and that there is enough free space on the destination disk to move it.

Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.