1. 程式人生 > >try-catch中的message提示

try-catch中的message提示

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 WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.TextLength == 0 || textBox2.TextLength == 0)
            {
                MessageBox.Show("請輸入數:");
                return;
            }
            try
            {
                double x = double.Parse(textBox1.Text);
                double y = double.Parse(textBox2.Text);
                double xy = x + y;
                textBox3.Text = xy.ToString();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);//自動顯示錯誤的原因

            }
        }
    }
}

執行結果: