Appendix O
Useful Exception Classes
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 18, “Error Handling,” and Appendix F.
The following table lists some of the most useful exception classes in Visual Basic .NET. You can use one of these when you need to throw an error.
CLASS | PURPOSE |
AmbiguousMatchException | The program could not figure out which overloaded object method to use. |
ApplicationException | 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. |
ArgumentException | An argument is invalid. |
ArgumentNullException | An argument that cannot be Nothing has the value Nothing. |
ArgumentOutOfRangeException | An argument is out of its allowed range. |
ArithmeticException | An arithmetic, casting, or conversion operation has occurred. |
ArrayTypeMismatchException | The program tried to store the wrong type of item in an array. |
ConfigurationException | A configuration setting is invalid. |
ConstraintException | A data operation violates a database constraint. |
DataException | The ancestor class for ADO.NET exception classes. |
DirectoryNotFoundException | A needed directory is missing. |
DivideByZeroException | The program tried to divide by zero. |
DuplicateNameException | An ADO.NET operation encountered a duplicate name (for example, it tried to create two tables with the same name). |
EvaluateException | Occurs when a DataColumn’s Expression property cannot be evaluated. |
FieldAccessException | The program tried to access a class property improperly. |
FormatException | An argument’s format doesn’t match its required format. |
IndexOutOfRangeException | The program tried to access an item outside of the bounds of an array or other container. |
InvalidCastException | The program tried to make an invalid conversion. For example, Integer.Parse(“ten”). |
InvalidOperationException | The operation is not currently allowed. |
IOException | The ancestor class for input/output (I/O) exception classes. A generic I/O error occurred. |
EndOfStreamException | A stream reached its end. |
FileLoadException | Error loading a file. |
FileNotFoundException | Error finding a file. |
InternalBufferOverflowException | An internal buffer overflowed. |
MemberAccessException | The program tried to access a class member improperly. |
MethodAccessException | The program tried to access a class method improperly. |
MissingFieldException | The program tried to access a class field that doesn’t exist. |
MissingMemberException | The program tried to access a class member that doesn’t exist. |
MissingMethodException | The program tried to access a class method that doesn’t exist. |
NotFiniteNumberException | 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). |
NotImplementedException | The requested operation is not implemented. |
NotSupportedException | The requested operation is not supported. For example, the program might be asking a routine to modify data that was opened as read-only. |
NullReferenceException | The program tried to use an object reference that is Nothing. |
OutOfMemoryException | 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 cache, 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 cache. |
OverflowException | An arithmetic, casting, or conversion operation created an overflow. For example, the program tried to assign a large Integer value to a Byte variable. |
PolicyException | Policy prevents the code from running. |
RankException | A routine is trying to use an array with the wrong number of dimensions. |
ReadOnlyException | The program tried to modify read-only data. |
SecurityException | A security violation occurred. |
SyntaxErrorException | A DataColumn’s Expression property contains invalid syntax. |
Unauthorized Access Exception | 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:
Throw New DivideByZeroException("No employees are defined.")
This code 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.
To define a custom exception class, make a class that inherits from an exception class. To give developers who use the class the most flexibility, provide four constructors that delegate their work to the parent class’s corresponding constructors.
The following code shows the InvalidWorkAssignmentException class. The parameterless 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 ApplicationException
Public Sub New()
MyBase.New("This work assignment is invalid")
End Sub
Public Sub New(msg As String)
MyBase.New(msg)
End Sub
Public Sub New(msg As String, inner_exception As Exception)
MyBase.New(msg, inner_exception)
End Sub
Public Sub New(info As SerializationInfo, context As StreamingContext)
MyBase.New(info, context)
End Sub
End Class
For more information on custom exception classes, see Chapter 18 and the online documentation for topics such as “Designing Custom Exceptions” (http://msdn.microsoft.com/ms229064.aspx) and “Design Guidelines for Exceptions” (http://msdn.microsoft.com/ms229014.aspx), or search the web for articles such as “Custom Exceptions in VB 2005” by Josh Fitzgerald (http://www.developer.com/net/vb/article.php/3590931).