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]

Several catch branches


   

/*
Learning C# 
by Jesse Liberty

Publisher: O&#039;Reilly 
ISBN: 0596003765
*/

 using System;

 namespace ExceptionHandling
 {
    public class TesterExceptionHandling4
    {

       public void Run()
       {
           try
           {
               double a = 5;
               double b = 0;
               Console.WriteLine("Dividing {0} by {1}...",a,b);
               Console.WriteLine ("{0} / {1} = {2}",
                   a, b, DoDivide(a,b));
           }

               // most derived exception type first
           catch (System.DivideByZeroException)
           {
               Console.WriteLine(
                   "DivideByZeroException caught!");
           }

           catch (System.ArithmeticException)
           {
               Console.WriteLine(
                   "ArithmeticException caught!");
           }

               // generic exception type last
           catch
           {
               Console.WriteLine(
                   "Unknown exception caught");
           }
       }

        // do the division if legal
        public double DoDivide(double a, double b)
        {
            if (b == 0)
                throw new System.DivideByZeroException();
            if (a == 0)
                throw new System.ArithmeticException();
            return a/b;
        }


        static void Main()
        {
            Console.WriteLine("Enter Main...");
            TesterExceptionHandling4 t = new TesterExceptionHandling4();
            t.Run();
            Console.WriteLine("Exit Main...");
        }


    }
 }