illustrates exception propagation with methods : Exception Try Catch « Language Basics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Language Basics » Exception Try CatchScreenshots 
illustrates exception propagation with methods
illustrates exception propagation with methods

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example13_6.cs illustrates exception propagation
  with methods
*/

using System;


// declare the ExceptionsTest class
class ExceptionsTest
{

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

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

}


public class Example13_6
{

  public static void Main()
  {

    ExceptionsTest myExceptionsTest = new ExceptionsTest();

    // call the AccessInvalidArrayElement() method,
    // this method handles the exception locally
    Console.WriteLine("Calling AccessInvalidArrayElement()");
    myExceptionsTest.AccessInvalidArrayElement();

    try
    {

      // call the DivideByZero() method,
      // this method doesn't handle the exception locally and
      // so it must be handled here
      Console.WriteLine("Calling DivideByZero()");
      myExceptionsTest.DivideByZero();

    }
    catch (DivideByZeroException e)
    {

      Console.WriteLine("Handling an IndexOutOfRangeException");
      Console.WriteLine("Message = " + e.Message);
      Console.WriteLine("StackTrace = " + e.StackTrace);

    }

  }

}

           
       
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.Catch Divide By Zero ExceptionCatch Divide By Zero Exception
7.Demonstrates stacking catch blocks to provide alternate code for more than one exception type
8.Catches an exception that was thrown in a component
9.Throw a format exception purposely to demonstrate catching a FormatException
10.Demonstrates using if statements to sort out an IOException
11.Several catch branchesSeveral catch branches
12.Demonstrate exception handlingDemonstrate exception handling
13.An exception can be generated by one method and caught by anotherAn exception can be generated by one 
   method and caught by another
14.Exception Type MismatchException Type Mismatch
15.Handle error gracefully and continueHandle error gracefully and continue
16.Use multiple catch statementsUse multiple catch statements
17.Use the 'catch all' catch statementUse the 'catch all' catch statement
18.Use a nested try blockUse a nested try block
19.Catch different exceptions
20.Passing Exceptions on to the Caller: Caller Confuse
21.Passing Exceptions on to the Caller: Caller Inform
22.Exception Handling:Trying and CatchingException Handling:Trying and Catching
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.