When to Raise Errors
There are four common situations in which your components might raise errors. Let’s take a look:
-
The good news is that most of the time you don’t have to do anything. If the user passes an invalid argument or does some other bozo illegal thing when calling your component, you can ignore it. Visual Basic will automatically raise the appropriate error. Since you didn’t handle the error, it will fall through to the client. It’s the client’s problem, not yours.
-
Often the errors raised by illegal operations in your components won’t make sense to clients. You might get the error “Object doesn’t support this property” inside your component method, but you don’t want to pass it back that way because you know from the context exactly which property is expected. You can create and raise a new error with a more specific message.
-
Some errors you detect in your component might map to standard Visual Basic run-time errors. If you ask for a filename parameter and the user passes an invalid file, you can raise the standard “File not found” error rather than creating your own. This is a judgment call. Sometimes you might want to create your own more specific message.
-
When you detect errors from API functions, you’ll usually want to generate your own error messages, but you don’t necessarily have to do it from scratch. Windows has an embedded string for every API error, and you can reuse those strings as your error descriptions.