Examine compiler-generated exceptions
- 10 minutes
Exceptions are generated by the .NET runtime or by code in a program. The exception type is dependent on the code that causes the exception.
Compiler-generated exceptions
The .NET runtime throws exceptions when basic operations fail. Here's a short list of runtime exceptions and their error conditions:
ArrayTypeMismatchException: Thrown when an array can't 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.FormatException: Thrown when the format of an argument is invalid.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 an attempt is made to reference an object whose value is null.OverflowException: Thrown when an arithmetic operation in a checked context overflows.
Code samples for compiler-generated exceptions
The following code samples show an example of the code that causes a compiler-generated exception.
ArrayTypeMismatchException
An exception of type ArrayTypeMismatchException is thrown when an attempt is made to store an element of the wrong type within an array. The following example throws an ArrayTypeMismatchException exception when trying to store a numeric value in a string array.
string[] names = { "Dog", "Cat", "Fish" };
Object[] objs = (Object[])names;
Object obj = (Object)13;
objs[2] = obj; // ArrayTypeMismatchException occurs
DivideByZeroException
An exception of type DivideByZeroException occurs when trying to divide an integer or Decimal number by zero. The following example throws a DivideByZeroException exception when performing integer division.
int number1 = 3000;
int number2 = 0;
Console.WriteLine(number1 / number2); // DivideByZeroException occurs
Note
Dividing a floating-point value by zero doesn't throw an exception; it results in positive infinity, negative infinity, or not a number (NaN), according to the rules of IEEE 754 arithmetic.
FormatException
An exception of type FormatException occurs when the format of an argument is invalid, or when a composite format string is not well formed. The following example throws a FormatException exception when trying to convert a string to an integer.
int valueEntered;
string userValue = "two";
valueEntered = int.Parse(userValue); // FormatException occurs
IndexOutOfRangeException
An exception of type IndexOutOfRangeException is thrown when an attempt is made to access an element of an array or collection with an index that is outside its bounds. The following example throws an IndexOutOfRangeException exception when attempting to assign the last element of the values1 array to the last element of the values2 array.
int[] values1 = { 3, 6, 9, 12, 15, 18, 21 };
int[] values2 = new int[6];
values2[values1.Length - 1] = values1[values1.Length - 1]; // IndexOutOfRangeException occurs
InvalidCastException
An exception of type InvalidCastException is thrown when attempting an invalid casting or explicit conversion. The following example throws an InvalidCastException when attempting to cast an object of type string to an int variable.
object obj = "This is a string";
int num = (int)obj;
NullReferenceException
An exception of type NullReferenceException is thrown when attempting to access a member on a type whose value is null. Two examples are shown. In the first example, a NullReferenceException is thrown when attempting to access an element of a null array. The second example throws a NullReferenceException when attempting to access a method of a null string.
int[] values = null;
for (int i = 0; i <= 9; i++)
values[i] = i * 2;
string? lowCaseString = null;
Console.WriteLine(lowCaseString.ToUpper());
OverflowException
An exception of type OverflowException occurs when an arithmetic operation attempts to assign a result that's outside the range of the target data type. The following example throws an OverflowException exception when attempting to convert a decimal value of 400 to a byte variable.
decimal x = 400;
byte i;
i = (byte)x; // OverflowException occurs
Console.WriteLine(i);
Recap
Here are a few important things to remember from this unit:
- The .NET runtime throws an exception when an operation fails.
- The exception type is dependent on the code that causes the exception.
- Your application should catch runtime exceptions.