Mega Code Archive

 
Categories / C# / Language Basics
 

Passing Exceptions on to the Caller

/* 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));         }     } }