/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example3_1.cs illustrates the use of
expressions to calculate and display
the circumference of a circle
*/
public class Example3_1
{
public static void Main()
{
const double Pi = 3.14159;
double diameter = 2.5;
// calculate the circumference
double circumference = Pi * diameter;
// display the circumference
System.Console.WriteLine("Circumference = " + circumference);
}
}
Language Basics
Demonstrates the use of Environment.Exit() in a command line program
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// EnvExit.cs -- Demonstrates the use of Environment.Exit() in
// a command line program.
//
// Compile this program with the following command line:
// C:>csc EnvExit.cs
namespace nsEnvExit
{
using System;
public class EnvExit
{
static public void Main ()
{
FirstFunction ();
Console.WriteLine ("Application ends");
}
static public void FirstFunction()
{
SecondFunction ();
Console.WriteLine ("First Function ends");
}
static public void SecondFunction()
{
ThirdFunction ();
Console.WriteLine ("First Function ends");
}
static public void ThirdFunction()
{
try
{
Exception e = new Exception ();
throw (e);
}
catch (Exception)
{
Console.WriteLine ("No other lines should print " +
"to the console");
Environment.Exit (-1);
}
}
}
}
Exception Handling:Trying and Catching
using System;
public class TryingCatching
{
static int Zero = 0;
public static void Main()
{
// watch for exceptions here
try
{
int j = 22 / Zero;
}
// exceptions that occur in try are transferred here
catch (Exception e)
{
Console.WriteLine("Exception " + e.Message);
}
Console.WriteLine("After catch");
}
}
Passing Exceptions on to the Caller: Caller Inform
/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 04 - Exception HandlingPassing Exceptions on to the CallerCaller Inform
// copyright 2000 Eric Gunnerson
using System;
public class CallerInform
{
public static void Main()
{
Summer summer = new Summer();
try
{
summer.DoAverage();
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}", e);
}
}
}
public class Summer
{
int sum = 0;
int count = 0;
float average;
public void DoAverage()
{
try
{
average = sum / count;
}
catch (DivideByZeroException e)
{
// wrap exception in another one,
// adding additional context.
throw (new DivideByZeroException(
"Count is zero in DoAverage()", e));
}
}
}
Passing Exceptions on to the Caller: Caller Confuse
/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 04 - Exception HandlingPassing Exceptions on to the CallerCaller Confuse
// copyright 2000 Eric Gunnerson
using System;
public class CallerConfuse
{
public static void Main()
{
Summer summer = new Summer();
try
{
summer.DoAverage();
}
catch (Exception e)
{
Console.WriteLine("Exception {0}", e);
}
}
}
public class Summer
{
int sum = 0;
int count = 0;
float average;
public void DoAverage()
{
try
{
average = sum / count;
}
catch (DivideByZeroException e)
{
// do some cleanup here
throw;
}
}
}
Catch different exceptions
/*
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
/*
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]