/* 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); } } }
Author: coder
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
Manually throw an exception
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Manually throw an exception. using System; public class ThrowDemo { public static void Main() { try { Console.WriteLine("Before throw."); throw new DivideByZeroException(); } catch (DivideByZeroException) { // catch the exception Console.WriteLine("Exception caught."); } Console.WriteLine("After try/catch block."); } }