336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

목표: 003 프로젝트에 예외를 처리 한다.

 

소스코드 : 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

    public enum Arithmetic
    {
        plus,
        minus,
        multiple,
        divison
    }
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.Clear();
        }

        private double GetNum(string str)
        {
            double returnValue = 0;
            if(string.IsNullOrEmpty(str))
            {
                MessageBox.Show("공백 입니다, 숫자를 입력하세요");
                this.Clear();
                throw new Exception("공백 입니다, 숫자를 입력하세요");
            }

            try
            {
                returnValue = double.Parse(str);
            }
            catch
            {
                MessageBox.Show("숫자를 입력하세요");
                this.Clear();
                throw new Exception("숫자를 입력하세요");
            }

            return returnValue;
        }

        private double GetNum1()
        {
            return GetNum(this.textNum1.Text);
        }

        private double GetNum2()
        {
            return GetNum(this.textNum2.Text);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            double num1 = GetNum1();
            double num2 = GetNum2();
            double num = Operate(num1, num2, Arithmetic.plus);

            this.textResult.Text = num.ToString();
            this.lbResult.Text = "+";
        }
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.Clear();
        }

        private void Clear()
        {
            this.textNum1.Text = "";
            this.textNum2.Text = "";
            this.textResult.Text = "";
            this.lbResult.Text = "Ready";
        }

        private double Operate(double num1, double num2, Arithmetic ari)
        {
            double returnValue = 0.0;

            switch(ari)
            {
                case Arithmetic.plus:
                    returnValue = num1 + num2;
                    break;
                case Arithmetic.minus:
                    returnValue = num1 - num2;
                    break;
                case Arithmetic.multiple:
                    returnValue = num1 * num2;
                    break;
                case Arithmetic.divison:                    
                    returnValue = num1 / num2;
                    break;
            }

            return returnValue;
        }
    }
}

 

+ Recent posts