using System;
public class ExceptionHierarchyOutOfRangeException
{
static int Zero = 0;
static void AFunction()
{
try
{
int j = 22 / Zero;
}
// this exception doesn't match
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine("OutOfRangeException: {0}", e);
}
Console.WriteLine("In AFunction()");
}
public static void Main()
{
try
{
AFunction();
}
// this exception doesn't match
catch (ArgumentException e)
{
Console.WriteLine("ArgumentException {0}", e);
}
}
}
Language Basics
Exception Handling: The Exception Hierarchy 2
using System;
public class ExceptionHierarchyNeverExecuted
{
static int Zero = 0;
static void AFunction()
{
int j = 22 / Zero;
Console.WriteLine("In AFunction()");
}
public static void Main()
{
try
{
AFunction();
}
catch (DivideByZeroException e)
{
Console.WriteLine("DivideByZero {0}", e);
}
}
}
Exception Handling: The Exception Hierarchy 1
using System;
public class ExceptionHierarchy
{
static int Zero = 0;
public static void Main()
{
try
{
int j = 22 / Zero;
}
// catch a specific exception
catch (DivideByZeroException e)
{
Console.WriteLine("DivideByZero {0}", e);
}
// catch any remaining exceptions
catch (Exception e)
{
Console.WriteLine("Exception {0}", e);
}
}
}
Using checked and unchecked with statement blocks
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Using checked and unchecked with statement blocks.
using System;
public class CheckedBlocks {
public static void Main() {
byte a, b;
byte result;
a = 127;
b = 127;
try {
unchecked {
a = 127;
b = 127;
result = unchecked((byte)(a * b));
Console.WriteLine("Unchecked result: " + result);
a = 125;
b = 5;
result = unchecked((byte)(a * b));
Console.WriteLine("Unchecked result: " + result);
}
checked {
a = 2;
b = 7;
result = checked((byte)(a * b)); // this is OK
Console.WriteLine("Checked result: " + result);
a = 127;
b = 127;
result = checked((byte)(a * b)); // this causes exception
Console.WriteLine("Checked result: " + result); // won't execute
}
}
catch (OverflowException exc) {
// catch the exception
Console.WriteLine(exc);
}
}
}
refines the System.Exception base class with name of the type and time of exception to create your own Exception
using System;
public class ConstructorException : Exception {
public ConstructorException(object origin)
: this(origin, null) {
}
public ConstructorException(object origin, Exception innerException)
: base("Exception in constructor", innerException) {
prop_Typename = origin.GetType().Name;
prop_Time = DateTime.Now.ToLongDateString() + " " +
DateTime.Now.ToShortTimeString();
}
protected string prop_Typename = null;
public string Typename {
get {
return prop_Typename;
}
}
protected string prop_Time = null;
public string Time {
get {
return prop_Time;
}
}
}
public class Starter {
public static void Main() {
try {
MyClass obj = new MyClass();
} catch (ConstructorException except) {
Console.WriteLine(except.Message);
Console.WriteLine("Typename: " + except.Typename);
Console.WriteLine("Occured: " + except.Time);
}
}
}
class MyClass {
public MyClass() {
// initialization fails
throw new ConstructorException(this);
}
}
Using checked and unchecked
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Using checked and unchecked.
using System;
public class CheckedDemo {
public static void Main() {
byte a, b;
byte result;
a = 127;
b = 127;
try {
result = unchecked((byte)(a * b));
Console.WriteLine("Unchecked result: " + result);
result = checked((byte)(a * b)); // this causes exception
Console.WriteLine("Checked result: " + result); // won't execute
}
catch (OverflowException exc) {
// catch the exception
Console.WriteLine(exc);
}
}
}
uses some of the properties of the Exception class
using System;
using System.Reflection;
public class Starter {
public static bool bException = true;
public static void Main() {
try {
MethodA();
} catch (Exception except) {
Console.WriteLine(except.Message);
bException = false;
except.TargetSite.Invoke(null, null);
}
}
public static void MethodA() {
if (bException) {
throw new ApplicationException("exception message");
}
}
}