##ReadMore##
檔案 → 新增專案 → 主控台應用程式 → 撰寫 Program.cs
練習一:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lesson_A._7
{
class Program
{
static void Main(string[] args)
{
int i, k, p;
i = 5;
k = 0;
Console.WriteLine("計算:" + i + " / " + k);
try
{
// 嘗試可能發生例外處理的程式
p = i / k;
}
catch (Exception ex)
{
// 當例外發生時的處理方式
Console.WriteLine("發生例外");
Console.WriteLine(ex.Message);
}
finally
{
// finally 不管有無發生 Exception 都會執行。
Console.WriteLine("...沒中斷繼續...");
}
for (i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
Console.WriteLine("The program is over.");
Console.Read();
}
}
}
練習二
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lesson_A._7_test
{
class Program
{
static void Main(string[] args)
{
int i = 10;
for (; i >= -10; i--)
{
try
{
Console.WriteLine(" 100 ÷ {0} = {1}…{2}", i, 100 / i, 100 % i);
}
catch (Exception ex)
{
Console.WriteLine("例外訊息:{0}", ex.Message);
Console.WriteLine("發生例外的函式:{0}", ex.StackTrace);
Console.WriteLine("發生例外的物件:{0}", ex.Source);
Console.WriteLine("發生例外的物件型別:{0}", ex.GetType());
Console.WriteLine("發生例外的文字說明:{0}", ex.ToString());
}
}
Console.Read();
}
}
}
沒有留言:
張貼留言