Appendix N : Useful Exception Classes


When your program throws an exception, it’s easy enough to use a Try Catch 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 8 and Appendix F.

Standard Exception Classes

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.

Open table as spreadsheet

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 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 a second table 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(“oops”).

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 property 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 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.

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 RankException

Policy prevents the code from running. 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.

UnauthorizedAccessException

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.") 




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net