例外處理
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ex2
{
    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 = "錯誤";
            }

        }
    }
}

執行結果
ex2-1.JPG
ex2-2.JPG

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License