logo search
CSharp_Prog_Guide

Определение классов исключений

Программы могут генерировать предопределенный класс исключений в пространстве имен System (если специально не обозначено иное), или создавать собственные классы исключений путем наследования от ApplicationException. Производные классы должны определять, по меньшей мере, четыре конструктора: один конструктор по умолчанию, один конструктор, задающий свойство сообщения, и еще один, задающий свойства Message и InnerException. Четвертый конструктор служит для сериализации исключения. Новые классы исключений должны быть сериализуемыми. Пример.

-----

Новые свойства следует добавлять к классу исключений только в том случае, если данные в них могут помочь в разрешении исключения. При добавлении новых свойств в производный класс исключений метод ToString() должен быть переопределен для возврата добавленных сведений.

Compiler-Generated Exceptions

Some exceptions are thrown automatically by the .NET Framework's common language runtime (CLR) when basic operations fail. These exceptions and their error conditions are listed in the following table.

Exception

Description

ArithmeticException

A base class for exceptions that occur during arithmetic operations, such as DivideByZeroException and OverflowException.

ArrayTypeMismatchException

Thrown when an array cannot store a given element because the actual type of the element is incompatible with the actual type of the array.

DivideByZeroException

Thrown when an attempt is made to divide an integral value by zero.

IndexOutOfRangeException

Thrown when an attempt is made to index an array when the index is less than zero or outside the bounds of the array.

InvalidCastException

Thrown when an explicit conversion from a base type to an interface or to a derived type fails at runtime.

NullReferenceException

Thrown when you attempt to reference an object whose value is null.

OutOfMemoryException

Thrown when an attempt to allocate memory using the new operator fails. This indicates that the memory available to the common language runtime has been exhausted.

OverflowException

Thrown when an arithmetic operation in a checked context overflows.

StackOverflowException

Thrown when the execution stack is exhausted by having too many pending method calls; usually indicates a very deep or infinite recursion.

TypeInitializationException

Thrown when a static constructor throws an exception and no compatible catch clause exists to catch it.