You are on page 1of 5

DivideByZeroException

using System;

class MainClass{

public static void Main(){

int Zero = 0;

try {

int j = 22 / Zero;

} catch (DivideByZeroException e) // catch a specific exception

Console.WriteLine("DivideByZero {0}", e);

} catch (Exception e)// catch any remaining exceptions

Console.WriteLine("Exception {0}", e);

The System.Exception Class


1. In C#, exceptions are represented by classes.
2. All exception classes must be derived from the built-in exception class

System.Exception.

3. SystemException class and ApplicationException class are derived from System.Exception.


4. SystemException support exceptions generated by the C# runtime system
(that is, the Common Language Runtime)

5. ApplicationException support exceptions generated by application programs.


6. SystemException and ApplicationException add nothing to Exception.
7. SystemException and ApplicationException define the tops of two different exception

hierarchies.

A Closer Look at Exception

1. Message property contains a string that describes the nature of the error.
2. StackTrace property contains a string that contains the stack of calls that lead to the exception.

3. TargetSite property obtains an object that specifies the method that generated the exception.
System.Exception also defines several methods.
1. ToString() returns a string that describes the exception.

2. Exception defines four constructors.


The most commonly used are shown here:
Exception()
Exception(string str)

using System;

class MainClass {
public static void Main() {

try {
int[] nums = new int[4];

Console.WriteLine("Before exception is generated.");

// Generate an index out-of-bounds exception.


for(int i=0; i < 10; i++) {
nums[i] = i;
}
}
catch (IndexOutOfRangeException exc) {
Console.WriteLine("Standard message is: ");
Console.WriteLine(exc); // calls ToString()
Console.WriteLine("Stack trace: " + exc.StackTrace);
Console.WriteLine("Message: " + exc.Message);
Console.WriteLine("TargetSite: " + exc.TargetSite);
Console.WriteLine("Class defining member: {0}", exc.TargetSite.DeclaringType);
Console.WriteLine("Member type: {0}", exc.TargetSite.MemberType);
Console.WriteLine("Source: {0}", exc.Source);
Console.WriteLine("Help Link: {0}", exc.HelpLink);

}
Console.WriteLine("After catch statement.");
}
}

Before exception is generated.


Standard message is:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at MainClass.Main()
Stack trace: at MainClass.Main()
Message: Index was outside the bounds of the array.
TargetSite: Void Main()
Class defining member: MainClass
Member type: Method
Source: main
Help Link:
After catch statement.

Exception propagation with methods

using System;

class MainClass
{
public static void Main()
{
Console.WriteLine("Calling AccessInvalidArrayElement()");
AccessInvalidArrayElement();

try
{
Console.WriteLine("Calling DivideByZero()");
DivideByZero();

} catch (DivideByZeroException e) {
Console.WriteLine("Handling an IndexOutOfRangeException");
Console.WriteLine("Message = " + e.Message);
Console.WriteLine("StackTrace = " + e.StackTrace);
}
}
public static void AccessInvalidArrayElement()
{
int[] myArray = new int[2];
try
{
Console.WriteLine("Attempting to access an invalid array element");
myArray[20] = 1;
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("Handling an IndexOutOfRangeException");
Console.WriteLine("Message = " + e.Message);
Console.WriteLine("StackTrace = " + e.StackTrace);
}
}

public static void DivideByZero()


{
int zero = 0;
Console.WriteLine("Attempting division by zero");
int myInt = 1 / zero;
}

Calling AccessInvalidArrayElement()
Attempting to access an invalid array element
Handling an IndexOutOfRangeException
Message = Index was outside the bounds of the array.
StackTrace = at MainClass.AccessInvalidArrayElement()
Calling DivideByZero()
Attempting division by zero
Handling an IndexOutOfRangeException
Message = Attempted to divide by zero.
StackTrace = at MainClass.Main()
Commonly Used Exceptions Defined Within the System Namespace

Exception Meaning
ArrayTypeMismatchExceptio
Type is incompatible with the type of the array.
n
DivideByZeroException Division by zero attempted.
IndexOutOfRangeException Array index is out of bounds.
InvalidCastException A runtime cast is invalid.
OutOfMemoryException Insufficient free memory exists.
OverflowException An arithmetic overflow occurred.
An attempt was made to operate on a null reference?that is,
NullReferenceException
a reference that does not refer to an object.
StackOverflowException The stack was Overflow.
As a general rule, exceptions defined by you should be derived from
ApplicationException since this is the
hierarchy reserved for application- related exceptions.

You might also like