0% found this document useful (0 votes)
60 views

Represents Errors That Occur During Application Execution

An IndexOutOfRangeException is thrown when an invalid index is used to access an array or collection. It inherits from the Exception class but adds no unique members.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Represents Errors That Occur During Application Execution

An IndexOutOfRangeException is thrown when an invalid index is used to access an array or collection. It inherits from the Exception class but adds no unique members.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

A. Represents errors that occur during application execution.

using System;

class ExceptionTestClass
{
public static void Main()
{
int x = 0;
try
{
int y = 100/x;
}
catch (ArithmeticException e)
{
Console.WriteLine("ArithmeticException Handler: {0}", e.ToString());
}
catch (Exception e)
{
Console.WriteLine("Generic Exception Handler: {0}", e.ToString());
}
}
}
/*
This code example produces the following results:

ArithmeticException Handler: System.DivideByZeroException: Attempted to divide


by zero.
at ExceptionTestClass.Main()

*/

B. The exception that is thrown when an I/O error occurs.


.
C# Syntax:
public IOException(
   string message
);
Parameters:
message
A String that describes the error. The content of message is
intended to be understood by humans. The caller of this constructor
is required to ensure that this string has been localized for the
current system culture.
Remarks
The constructor initializes the Exception.Message property of the new
instance using message.

C. An IndexOutOfRangeException exception is thrown when an invalid


index is used to access a member of an array or a collection, or to
read or write from a particular location in a buffer. This exception
inherits from the Exception class but adds no unique members.
using System;
using System.Collections.Generic;

public class Example


{
public static void Main()
{
List<Char> characters = new List<Char>();
characters.InsertRange(0, new Char[] { 'a', 'b', 'c', 'd', 'e', 'f' } );
for (int ctr = 0; ctr <= characters.Count; ctr++)
Console.Write("'{0}' ", characters[ctr]);
}
}
// The example displays the following output:
// 'a' 'b' 'c' 'd' 'e' 'f'
// Unhandled Exception:
// System.ArgumentOutOfRangeException:
// Index was out of range. Must be non-negative and less than the
size of the collection.
// Parameter name: index
// at Example.Main()
D. ArrayTypeMismatchException is thrown when the system cannot
convert the element to the type declared for the array. For example,
an element of type String cannot be stored in an Int32array because
conversion between these types is not supported. It is generally
unnecessary for applications to throw this exception. In the
following example, names is declared but never instantiated:
using System;
using System.Collections.Generic;

public class Example


{
public static void Main(string[] args)
{
int value = Int32.Parse(args[0]);
List<String> names;
if (value > 0)
names = new List<String>();

names.Add("Major Major Major");


}
}
// Compilation displays a warning like the following:
// Example1.cs(10) : warning BC42104: Variable //names// is used before it
// has been assigned a value. A null reference exception could result
// at runtime.
//
// names.Add("Major Major Major")
// ~~~~~
// The example displays output like the following output:
// Unhandled Exception: System.NullReferenceException: Object reference
// not set to an instance of an object.
// at Example.Main()

E. A NullReferenceException exception is thrown when you try to


access a member on a type whose value is null.
A NullReferenceException exception typically reflects developer
error and is thrown in the following scenarios
using System;
using System.Collections.Generic;

public class Example


{
public static void Main(string[] args)
{
int value = Int32.Parse(args[0]);
List<String> names;
if (value > 0)
names = new List<String>();

names.Add("Major Major Major");


}
}
// Compilation displays a warning like the following:
// Example1.cs(10) : warning BC42104: Variable //names// is used before it
// has been assigned a value. A null reference exception could result
// at runtime.
//
// names.Add("Major Major Major")
// ~~~~~
// The example displays output like the following output:
// Unhandled Exception: System.NullReferenceException: Object reference
// not set to an instance of an object.
// at Example.Main()

F. The exception that is thrown when there is an attempt to divide an


integral or Decimal value by zero. Trying to divide an integer
or Decimal number by zero throws
a DivideByZeroExceptionexception. To prevent the exception,
ensure that the denominator in a division operation with integer
or Decimal values is non-zero.
G. An InvalidCastException exception is thrown when the conversion
of an instance of one type to another type is not supported. For
example, attempting to convert a Char value to a DateTimevalue
throws an InvalidCastException exception
using System;

public class Example


{
public static void Main()
{
object value = 12;
// Cast throws an InvalidCastException exception.
string s = (string) value;
}
}

The message that indicates the reason the exception occurred.


For example: this will appear
System.InvalidCastException: Specified cast is not valid.
at example.MainClass.Main (System.String[] args) [0x0000a] in
/Users/tendairanga/Projects/example/example/Program.cs:14

H. OutOfMemoryException is the exception that is thrown when there


is not enough memory to continue the execution of a program.
using System;
using System.Collections.Generic;

public class Example


{
public static void Main()
{
Double[] values = GetData();
// Compute mean.
Console.WriteLine("Sample mean: {0}, N = {1}",
GetMean(values), values.Length);
}

private static Double[] GetData()


{
Random rnd = new Random();
List<Double> values = new List<Double>();
for (int ctr = 1; ctr <= 200000000; ctr++) {
values.Add(rnd.NextDouble());
if (ctr % 10000000 == 0)
Console.WriteLine("Retrieved {0:N0} items of data.",
ctr);
}
return values.ToArray();
}

private static Double GetMean(Double[] values)


{
Double sum = 0;
foreach (var value in values)
sum += value;

return sum / values.Length;


}
}
// The example displays output like the following:
// Retrieved 10,000,000 items of data.
// Retrieved 20,000,000 items of data.
// Retrieved 30,000,000 items of data.
// Retrieved 40,000,000 items of data.
// Retrieved 50,000,000 items of data.
// Retrieved 60,000,000 items of data.
// Retrieved 70,000,000 items of data.
// Retrieved 80,000,000 items of data.
// Retrieved 90,000,000 items of data.
// Retrieved 100,000,000 items of data.
// Retrieved 110,000,000 items of data.
// Retrieved 120,000,000 items of data.
// Retrieved 130,000,000 items of data.
//
// Unhandled Exception: OutOfMemoryException.

I. StackOverflowException is thrown for execution stack overflow errors,


typically in case of a very deep or unbounded recursion. It is thrown
as a direct result of a previous exception can include a reference to
the previous exception in the InnerException property.
The InnerExceptionproperty returns the same value that is passed
into the constructor, or a null reference (Nothing in Visual Basic) if
the InnerException property does not supply the inner exception
value to the constructor.

You might also like