When your program throws an exception, it's easy enough to use a TryCatch block to catch the exception and examine it to determine its class. When you want to throw your own exception, however, you must know what exception classes are available so that you can pick the right one.
For more information on error handling, see Chapter 19, "Error Handling," and Appendix F.
The following table lists some of the most useful exception classes in Visual Basic .NET. You can raise one of these when you need to throw an error.
CLASS | PURPOSE |
---|---|
| The program could not figure out which overloaded object method to use. |
| This is the ancestor class for all nonfatal application errors. When you build custom exception classes, you should inherit from this class, or from one of its descendants. |
| An argument is invalid. |
| An argument that cannot be Nothing has value Nothing. |
| An argument is out of its allowed range. |
| An arithmetic, casting, or conversion operation has occurred. |
| The program tried to store the wrong type of item in an array. |
| A configuration setting is invalid. |
| A data operation violates a database constraint. |
| The ancestor class for ADO.NET exception classes. |
| A needed directory is missing. |
| The program tried to divide by zero. |
| An ADO.NET operation encountered a duplicate name (for example, it tried to create a second table with the same name). |
| Occurs when a DataColumn's Expression property cannot be evaluated. |
| The program tried to access a class property improperly. |
| An argument's format doesn't match its required format. |
| The program tried to access an item outside of the bounds of an array or other container. |
| The program tried to make an invalid conversion. For example, Integer.Parse("oops"). |
| The operation is not currently allowed. |
| The ancestor class for input/output (I/O) exception classes. A generic I/O error occurred. |
| A stream reached its end. |
| Error loading a file. |
| Error finding a file. |
| An internal buffer overflowed. |
| The program tried to access a class member improperly. |
| The program tried to access a class method improperly. |
| The program tried to access a class field that doesn't exist. |
| The program tried to access a class member that doesn't exist. |
| The program tried to access a class method that doesn't exist. |
| A floating-point number is PositiveInfinity, NegativeInfinity, or NaN (Not a Number). You can get these values from the floating-point classes (as in Single.Nan or Double.PositiveInfinity). |
| The requested operation is not implemented. |
| The requested operation is not supported. For example, the program might be asking a routine to modify data that was opened as read-only. |
| The program tried to use an object reference that is Nothing. |
| There isn't enough memory. Note that sometimes a program cannot recover from an OutOfMemoryException because it doesn't have enough memory to do anything useful. This exception is most useful if you can predict beforehand that you will run out of memory before you actually use up all of the memory and crash the program. For example, if the user wants to generate a really huge data set, you may be able to predict how much memory the program will need, see if it is available, and throw this error without actually allocating the data set. |
| An arithmetic, casting, or conversion operation created an overflow. For example, the program tried to assign a large Integer value to a Byte variable. |
| Policy prevents the code from running. |
| A routine is trying to use an array with the wrong number of dimensions. |
| The program tried to modify read-only data. |
| A security violation occurred. |
| A DataColumn's Expression property contains invalid syntax. |
| The system is denying access because of an I/O or security error. |
Use the Throw statement to raise an exception. The following code throws a DivideByZeroException. It passes the exception class's constructor a message describing the exception. In this case, the divide by zero exception occurred because the application did not have any employees defined. Notice that the message explains the reason for the exception, not the mere fact that a division by zero occurred.
Throw New DivideByZeroException("No employees are defined.")
To define a custom exception class, make a class that inherits from Exception. To give developers who use the class the most flexibility, provide four constructors that delegate their work to the Exception class's corresponding constructors.
The following code shows the InvalidWorkAssignmentException class. The empty constructor passes the Exception class's constructor a default error message. The other constructors simply pass their arguments to the Exception class's other constructors.
Public Class InvalidWorkAssignmentException Inherits Exception Public Sub New() MyBase.New("This work assignment is invalid") End Sub Public Sub New(ByVal msg As String) MyBase.New(msg) End Sub Public Sub New(ByVal msg As String, ByVal inner_exception As Exception) MyBase.New(msg, inner_exception) End Sub Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext) MyBase.New(info, context) End Sub End Class
For more information on custom exception classes, see Chapter 19 and the online documentation for topics such as "Designing Custom Exceptions" (msdn.microsoft.com/ms229064.aspx
) and "Design Guidelines for Exceptions" (msdn.microsoft.com/ms229014.aspx
), or search the Web for articles such as "Custom Exceptions in VB 2005" by Josh Fitzgerald (www.developer.com/net/vb/article.php/3590931
).