Passing Exceptions on to the Caller: Caller Inform : Exception Try Catch « Language Basics « C# / C Sharp






Passing Exceptions on to the Caller: Caller Inform

/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/

// 04 - Exception Handling\Passing Exceptions on to the Caller\Caller Inform
// copyright 2000 Eric Gunnerson
using System;

public class CallerInform
{
    public static void Main()
    {
        Summer summer = new Summer();
        try
        {
            summer.DoAverage();
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception: {0}", e);
        }
    }
}
public class Summer
{
    int    sum = 0;
    int    count = 0;
    float    average;
    public void DoAverage()
    {
        try
        {
            average = sum / count;
        }
        catch (DivideByZeroException e)
        {
            // wrap exception in another one,
            // adding additional context.
            throw (new DivideByZeroException(
            "Count is zero in DoAverage()", e));
        }
    }
}

           
       








Related examples in the same category

1.Catch Error
2.illustrates an unhandled exceptionillustrates an unhandled exception
3.illustrates how to handle a specific exceptionillustrates how to handle a specific exception
4.illustrates multiple catch blocksillustrates multiple catch blocks
5.illustrates a nested try/catch blockillustrates a nested try/catch block
6.illustrates exception propagation with methodsillustrates exception propagation with methods
7.Catch Divide By Zero ExceptionCatch Divide By Zero Exception
8.Demonstrates stacking catch blocks to provide alternate code for more than one exception type
9.Catches an exception that was thrown in a component
10.Throw a format exception purposely to demonstrate catching a FormatException
11.Demonstrates using if statements to sort out an IOException
12.Several catch branchesSeveral catch branches
13.Demonstrate exception handlingDemonstrate exception handling
14.An exception can be generated by one method and caught by anotherAn exception can be generated by one 
   method and caught by another
15.Exception Type MismatchException Type Mismatch
16.Handle error gracefully and continueHandle error gracefully and continue
17.Use multiple catch statementsUse multiple catch statements
18.Use the 'catch all' catch statementUse the 'catch all' catch statement
19.Use a nested try blockUse a nested try block
20.Catch different exceptions
21.Passing Exceptions on to the Caller: Caller Confuse
22.Exception Handling:Trying and CatchingException Handling:Trying and Catching