Catch different exceptions

image_pdfimage_print
   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

namespace nsCompare
{
    using System;
    public class Compare
    {
        static public void Main (string [] args)
        {
            int TestArg;
            try
            {
                TestArg = int.Parse (args[0]);
            }
            catch (FormatException)
            {
                Console.WriteLine ("Please enter a number value.");
                return;
            }
            catch (IndexOutOfRangeException)
            {
                Console.WriteLine ("Please enter an argument");
                return;
            }
            string str;
            str = TestArg > 10 ? "The test is true" : "The test is false";
            Console.WriteLine (str);
        }
    }
}

           
          


Use a nested try block

image_pdfimage_print

/*
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

image_pdfimage_print

/*
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

image_pdfimage_print

/*
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

image_pdfimage_print

/*
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

image_pdfimage_print

/*
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

image_pdfimage_print

/*
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]