Error handling

Write your own exception class

Exceptions in Visual Basic
Exceptions in C#

Exceptions

Always use exceptions to handle errors.
They are the standard way to handle errors.
No need to worry about handling the error on every level, or handling different layers. With exceptions the error will 'fall through'.
Just make sure there is a try … catch … at the highest level.

Logging

At the lowest level (where probably the error is generated), log the original exception.
Throw a new exception with a discription of the action. Eg. 'Error saving customer'

In every level, log the original error. Add comment to the original error.
Eg. string.format("{0} in invoice <{1}>", ex.message, invoice.number
-> 'Error saving customer in invoice <15>

At the highest level show the message to the user.

Messages

  • Wrong format 'fieldname'.
  • Error updating/inserting/deleting 'object'. See log for details.

Multiple layers

If there are more layers involved and you don't want to stop eg. a loop for the error, but want to process a message to the user, use events.
Events in Visual Basic
Events in C#

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License