Basics

Exceptions can be thrown in two different ways. A throw statement can be executed programmatically or it can happen at runtime where the processing of the C# statement causes the error (like dividing by zero). Below is a table of common exception classes:

EXCEPTION

DESCRIPTION

System.OutOfMemoryException

Thrown when an attempt to allocate memory (via new) fails.

System.StackOverflowException

Thrown when the execution stack is exhausted by having too many pending method calls;typically indicative of very deep or unbounded recursion.

System.NullReferenceException

Thrown when a null reference is used in a way that causes the referenced object to be required.

System.InvalidCastException

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

System.ArrayTypeMismatchException

Thrown when a store into an array fails because the actual type of the stored element is incompatible with the actual type of the array.

System.IndexOutOfRangeException

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

System.ArithmeticException

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

DecIareUseMethods.cs - Notepad

File Edit Format Help

catch (ArqumentNullException e) f | l

Console WnteL¡ne("'{0}' was the exception ". e Message)

public string EvaluateString(string s)|

throw new ArgumentNullExceptlonQ;

Create a function called EvaluateString.

Use this function to raise an ArgumentNullException

Save as the class name.

Note: See page 35 for more information on saving a file.

Microsoft Windows 2000 [Uersion 5.00.2195] CO Copyright 1985-2000 Microsoft Corp.

C: XCsharp>csc 1 np lenient Except ionHandling. cs

Microsoft <JD Uisual Ctt Compiler Uersion 7.00.9148 [CLR «

615]

Copyright <C> Microsoft Corp 2000. fill rights reserved.

C:\Csharp>ImplementExceptionHandling.exe 'Ualue cannot be null.' Mas the exception

Microsoft Windows 2000 [Uersion 5.00.2195] CO Copyright 1985-2000 Microsoft Corp.

nd Sett ingsSAdmii strator>cd C:\CSharp

C: XCsharp>csc 1 np lenient Except ionHandling. cs

Microsoft <JD Uisual Ctt Compiler Uersion 7.00.9148 [CLR «

615]

Copyright <C> Microsoft Corp 2000. fill rights reserved.

C:\Csharp>ImplementExceptionHandling.exe 'Ualue cannot be null.' Mas the exception

I The program displays the error that was raised.

error.

Was this article helpful?

0 0

Post a comment