April 2002
Intermediate to advanced
1024 pages
23h 26m
English
C# allows a programmer almost all of the functionality provided by the CLR. To demonstrate exception handling in a C# environment, look at the next example that generates exceptions of various types. Because the code is rather long, the listing has been split into small sections. The full source for Listings 15.4–15.8 is in the BasicExceptions directory. Listing 15.4 shows an example of how C# code can be used to catch a DivideByZeroException.
// Try a divide by zero
try
{
int x = 0;
x = 1/x;
}
catch(DivideByZeroException e)
{
// Dump interesting exception information
Console.WriteLine (e.ToString());
}
|
When the exception is caught, the output looks like this:
System.DivideByZeroException: ...
Read now
Unlock full access