/* 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[2] = 1; } 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 = 1 / 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); } } }
illustrates a nested try/catch block
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example13_5.cs illustrates a nested try/catch block; the nested if throws an exception that is propagated to the outer exception */ using System; public class Example13_5 { public static void Main() { try { // a nested try and catch block try { int[] myArray = new int[2]; Console.WriteLine("Attempting to access an invalid array element"); myArray[2] = 1; // throws the exception } catch (DivideByZeroException e) { // code that handles a DivideByZeroException Console.WriteLine("Handling a DivideByZeroException"); Console.WriteLine("Message = " + e.Message); Console.WriteLine("StackTrace = " + e.StackTrace); } } catch (IndexOutOfRangeException e) { // code that handles an IndexOutOfRangeException Console.WriteLine("Handling an IndexOutOfRangeException"); Console.WriteLine("Message = " + e.Message); Console.WriteLine("StackTrace = " + e.StackTrace); } } }
illustrates multiple catch blocks
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example13_4.cs illustrates multiple catch blocks */ using System; public class Example13_4 { public static void Main() { try { int[] myArray = new int[2]; Console.WriteLine("Attempting to access an invalid array element"); myArray[2] = 1; } catch (DivideByZeroException e) { // code that handles a DivideByZeroException Console.WriteLine("Handling a System.DivideByZeroException object"); Console.WriteLine("Message = " + e.Message); Console.WriteLine("StackTrace = " + e.StackTrace); } catch (IndexOutOfRangeException e) { // code that handles an IndexOutOfRangeException Console.WriteLine("Handling a System.IndexOutOfRangeException object"); Console.WriteLine("Message = " + e.Message); Console.WriteLine("StackTrace = " + e.StackTrace); } catch (Exception e) { // code that handles a generic Exception: all other exceptions Console.WriteLine("Handling a System.Exception object"); Console.WriteLine("Message = " + e.Message); Console.WriteLine("StackTrace = " + e.StackTrace); } } }
illustrates how to handle a specific exception
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example13_3.cs illustrates how to handle a specific exception */ using System; public class Example13_3 { public static void Main() { try { int zero = 0; Console.WriteLine("In try block: attempting division by zero"); int myInt = 1 / zero; // throws the exception } catch (DivideByZeroException myException) { // code that handles a DivideByZeroException Console.WriteLine("Message = " + myException.Message); Console.WriteLine("StackTrace = " + myException.StackTrace); } } }
illustrates an unhandled exception
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example13_7.cs illustrates an unhandled exception */ using System; public class Example13_7 { public static void Main() { int[] myArray = new int[2]; Console.WriteLine("Attempting to access an invalid array element"); myArray[2] = 1; } }
Catch Error
using System; public class CatchError { public static void Main() { int var1 = 1000, var2 = 0, var3; try { var3 = var1 / var2; } catch (ArithmeticException e) { Console.WriteLine("Exception: {0}", e.ToString()); var3 = -1; } catch (Exception e) { Console.WriteLine("Exception: {0}", e.ToString()); var3 = -2; } Console.WriteLine("The result is: {0}", var3); } }
Rethrow an exception
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Rethrow an exception.
using System;
class Rethrow {
public static void genException() {
// here, numer is longer than denom
int[] numer = { 4, 8, 16, 32, 64, 128, 256, 512 };
int[] denom = { 2, 0, 4, 4, 0, 8 };
for(int i=0; i