Error Handling

Ignoring Errors

When you write code, you may want your application to totally ignore any errors that occur in a certain procedure. You can do this by entering

On Error Resume Next

at the start point, where you want it to ignore errors. To re enable the error messages in that procedure type

On Error Goto 0

For example, the following code attempts to delete Test.txt from drive C:. The first attempt, errors are ignored, but the second time, an error occurs when the procedure attempts to delete the file (providing the file does not exist).

Sub Command1_Click()
   On Error Resume Next ' Ignore all errors from this point

   Kill "C:\test.txt"
   ' An error occurs if the file does not exist, but it is ignored
   ' The error number and message is printed to the debug window
   Debug.Print Err & " : " & Error

   On Error GoTo 0 ' Errors from this point onwards will cause a break
  
   Kill "C:\test.txt"
   ' An error occurs, and a message is displayed. Code execution stops.

End Sub

Err returns the last error number that occurred. Error returns the last error message. Kill deletes the specified file

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The question of whether computers can think is just like the question of whether submarines can swim.” - Edsger W. Dijkstra