Are there any shorter way of ignoring an error?
This is my code
Try
Directory.Delete(FolderName, True)
Catch
End Try
It simply tries to delete a folder if it exist. If It doesn't see the folder an error will occur which is what I'm trying to ignore. It is taking space and makes it cumbersome to scroll through the code
I have used On Error Resume Next
but it seems to ignore all error on the method and it looks like I'm using it wrong.
It would be appreciated if someone tell a shorter way of trying to delete a directory without worrying about null/directory not found exceptions
Thanks!
CodePudding user response:
I always code with the intention of preventing errors from happening, so I would have written that as
If Directory.Exists(folderName) = True Then
Directory.Delete(folderName, True)
End If
That said, I encourage you to get rid off the the attitude to "ignore errors". Instead embrace exception handling with Try/Catch/End Try
, as it finally let's you specifically handle error prone lines of code, which was a nightmare to deal with in old VB6.
The IDE also provides code folding for Try/Catch/End Try
blocks, so if you feel they're taking away too much space, fold 'em!
CodePudding user response:
I would create a method and call it where you want to try to delete a folder. This way you always only have one line.
Sub TryDeleteFolder(folderName As String)
Try
Directory.Delete(folderName, True)
Catch
End Try
End Sub