/// <summary>
/// 第五章:使用复合赋值和循环语句
/// </summary>
namespace Chapter5
{
class Program
{
static void Main(string[] args)
{
One();//5.1使用复合赋值操作符
Two();//5.2编写while语句
Three();//5.3编写for语句
Tour();//5.4编写do语句
Console.ReadLine();
}
private static void Tour()
{
int i = 0;
do
{
Console.WriteLine(i);
i++;
}
while (i < 10);
}
private static void Three()
{
//for语句三个部分都可以省略,布尔表达式默认为true
for(int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
}
private static void Two()
{
int i = 0;
while (i < 10)
{
Console.WriteLine(i);
i++;
}
}
private static void One()
{
int answer=0;
Console.WriteLine(answer += 42);
}
}
}
/// <summary>
/// 第六章:管理错误和异常
/// </summary>
namespace Chapter6
{
class Program
{
static void Main(string[] args)
{
//try catch的使用
Moudle.Class1 First = new Moudle.Class1 ();
First.Test();
//checked、unchecked
Moudle.Class2 Second = new Moudle.Class2();
Second.Test();
//抛出异常
Moudle.Class3 Third = new Moudle.Class3();
Third.Test();
//6.4使用finally块
Four();
Console.ReadLine();
}
private static void Four()
{
try
{
}
finally
{
//无论怎样都会执行
}
}
}
}
/// <summary>
/// 6.1尝试执行代码并捕捉异常
/// </summary>
namespace Chapter6.Moudle
{
public class Class1
{
public void Test()
{
bool catchErrors = true;
try
{
String test = "2147483648";
int leftHandSide = int.Parse(test);
}
catch (FormatException fEx)//捕捉异常
{
Console.WriteLine("类型异常。");
}
catch (OverflowException oEx)//多个catch处理
{
Console.WriteLine("范围超出异常。");
}
catch (Exception e) when (catchErrors == true)//筛选异常
//捕捉全部异常
//catch 可以省略它的名称
{
Console.WriteLine("异常。");
}
}
}
}
/// <summary>
/// 6.2使用checked和unchecked进行整数运算
/// </summary>
namespace Chapter6.Moudle
{
public class Class2
{
public void Test()
{
//6.2.1编写checked语句
int number = int.MaxValue;
checked//任何整数运算溢出都抛出OverflowException异常
{
int willThrow1 = number++;
Console.WriteLine("不会执行!");
}
unchecked//强制不检查
{
int willThrow1 = number++;
Console.WriteLine("会执行!");
}
//6.2.2编写checked表达式
int willThrow2 = checked(int.MaxValue - 1);
}
}
}
/// <summary>
/// 6.3抛出异常
/// </summary>
namespace Chapter6.Moudle
{
class Class3
{
internal void Test()
{
throw new NotImplementedException();
}
}
}