Thursday, January 16, 2014

Exception Handling in C# Form App



PROBLEM STATEMENT
Handle an exception found in a division method.

FORM 1:CODING:
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 Lab11
{
    public partial class Form1 : Form
    {
        int num1, num2, ans;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                num1 = Convert.ToInt32(textBox1.Text);
                num2 = Convert.ToInt32(textBox2.Text);
                ans = num1 / num2;
                textBox3.Text = Convert.ToString(ans);
            }
            catch (DivideByZeroException de)
            {
                MessageBox.Show("Enter any integer other than zero");
            }
            catch (FormatException fe)
            {
                MessageBox.Show(fe.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                label4.Text = "this is from finally and it always run";
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

     
    }
}


OUTPUT:



No comments:

Post a Comment