Use a nested try block

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Use a nested try block.

using System;

public class NestTrys {
public static void Main() {
// Here, numer is longer than denom.
int[] numer = { 4, 8, 16, 32, 64, 128, 256, 512 };
int[] denom = { 2, 0, 4, 4, 0, 8 };

try { // outer try
for(int i=0; i < numer.Length; i++) { try { // nested try Console.WriteLine(numer[i] + " / " + denom[i] + " is " + numer[i]/denom[i]); } catch (DivideByZeroException) { // catch the exception Console.WriteLine("Can't divide by Zero!"); } } } catch (IndexOutOfRangeException) { // catch the exception Console.WriteLine("No matching element found."); Console.WriteLine("Fatal error -- program terminated."); } } } [/csharp]

Use the 'catch all' catch statement

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Use the “catch all” catch statement.

using System;

public class ExcDemo5 {
public static void Main() {
// 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 < numer.Length; i++) { try { Console.WriteLine(numer[i] + " / " + denom[i] + " is " + numer[i]/denom[i]); } catch { Console.WriteLine("Some exception occurred."); } } } } [/csharp]

Use multiple catch statements

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Use multiple catch statements.

using System;

public class ExcDemo4 {
public static void Main() {
// 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 < numer.Length; i++) { try { Console.WriteLine(numer[i] + " / " + denom[i] + " is " + numer[i]/denom[i]); } catch (DivideByZeroException) { // catch the exception Console.WriteLine("Can't divide by Zero!"); } catch (IndexOutOfRangeException) { // catch the exception Console.WriteLine("No matching element found."); } } } } [/csharp]

Handle error gracefully and continue

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Handle error gracefully and continue.

using System;

public class ExcDemo3 {
public static void Main() {
int[] numer = { 4, 8, 16, 32, 64, 128 };
int[] denom = { 2, 0, 4, 4, 0, 8 };

for(int i=0; i < numer.Length; i++) { try { Console.WriteLine(numer[i] + " / " + denom[i] + " is " + numer[i]/denom[i]); } catch (DivideByZeroException) { // catch the exception Console.WriteLine("Can't divide by Zero!"); } } } } [/csharp]

Exception Type Mismatch

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// This won't work!

using System;

public class ExcTypeMismatch {
public static void Main() {
int[] nums = new int[4];

try {
Console.WriteLine(“Before exception is generated.”);

// Generate an index out-of-bounds exception.
for(int i=0; i < 10; i++) { nums[i] = i; Console.WriteLine("nums[{0}]: {1}", i, nums[i]); } Console.WriteLine("this won't be displayed"); } /* Can't catch an array boundary error with a DivideByZeroException. */ catch (DivideByZeroException) { // catch the exception Console.WriteLine("Index out-of-bounds!"); } Console.WriteLine("After catch statement."); } } [/csharp]

An exception can be generated by one method and caught by another

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

/* An exception can be generated by one
method and caught by another. */

using System;

class ExcTest {
// Generate an exception.
public static void genException() {
int[] nums = new int[4];

Console.WriteLine(“Before exception is generated.”);

// Generate an index out-of-bounds exception.
for(int i=0; i < 10; i++) { nums[i] = i; Console.WriteLine("nums[{0}]: {1}", i, nums[i]); } Console.WriteLine("this won't be displayed"); } } public class ExcDemo2 { public static void Main() { try { ExcTest.genException(); } catch (IndexOutOfRangeException) { // catch the exception Console.WriteLine("Index out-of-bounds!"); } Console.WriteLine("After catch statement."); } } [/csharp]

Demonstrate exception handling

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Demonstrate exception handling.

using System;

public class ExcDemo1 {
public static void Main() {
int[] nums = new int[4];

try {
Console.WriteLine(“Before exception is generated.”);

// Generate an index out-of-bounds exception.
for(int i=0; i < 10; i++) { nums[i] = i; Console.WriteLine("nums[{0}]: {1}", i, nums[i]); } Console.WriteLine("this won't be displayed"); } catch (IndexOutOfRangeException) { // catch the exception Console.WriteLine("Index out-of-bounds!"); } Console.WriteLine("After catch statement."); } } [/csharp]