專案下載:WError.zip

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WError
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                double x = double.Parse(textBox1.Text);
                double y = double.Parse(textBox2.Text);
                String op = comboBox1.Text;
                double result = 0.0;
                switch (op)
                {
                    case "+": result = x + y; break;
                    case "-": result = x - y; break;
                    case "*": result = x * y; break;
                    case "/": result = x / y; break;
                    default: throw new Exception("出現錯誤!");
                }
                textBox3.Text = result.ToString();
            }
            catch
            {
                textBox3.Text = "錯誤";
            }
        }
    }
}
	

文章轉載:陳鍾誠 (2010年10月26日),(網頁標題) C# 的例外處理範例 — 視窗版本,(網站標題) 免費電子書:C# 程式設計,2010年10月26日,取自 http://cs0.wikidot.com/exception2 ,網頁修改第 1 版。

arrow
arrow

    Johnson峰 發表在 痞客邦 留言(0) 人氣()