/*
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);
}
}
}
Month: February 2011
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]
Demonstrates rethrowing an exception from a method
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// Rethrow.cs -- Demonstrates rethrowing an exception from a method.
//
// Compile this program with the following command line:
// C:>csc Rethrow.cs
//
namespace nsRethrow
{
using System;
using System.IO;
public class Rethrow
{
static public void Main ()
{
while (true)
{
Console.Write ("Please enter a file name (return to exit): ");
string FileName = Console.ReadLine ();
if (FileName.Length == 0)
break;
try
{
ReadFile (FileName);
break;
}
catch (IOException e)
{
if (e is FileNotFoundException)
Console.WriteLine ("The file " + FileName + " was not found");
}
catch (Exception e)
{
Console.WriteLine (e.Message + "
");
break;
}
}
}
static public void ReadFile (string FileName)
{
FileStream strm;
StreamReader reader;
try
{
strm = new FileStream (FileName, FileMode.Open);
reader = new StreamReader (strm);
string str = reader.ReadToEnd ();
Console.WriteLine (str);
}
catch (IOException e)
{
// If file not found, go back and get another name
if (e is FileNotFoundException)
throw (e);
// Code here to handle other IOException classes
Console.WriteLine (e.Message);
throw (new IOException());
}
}
}
}

