/*
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);
}
}
}
Language Basics
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.");
}
}
Let the C# runtime system handle the error
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Let the C# runtime system handle the error.
using System;
public class NotHandled {
public static void Main() {
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]);
}
}
}
[/csharp]