VR Indian Wanderers Youtube RSS Feed
Learn with Vikas Suhag
Write a program that handles the Exceptions supported by C# .Net
Nov 08, 2021   Vikas Suhag

Exception handling in C#

Write a program that handles the Exceptions supported by C# .Net

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 Exceptions
{
    public partial class Form1 : Form
    {
        int a, b, c;
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            a = Int32.Parse(textBox1.Text);
            b = Int32.Parse(textBox2.Text);
            try
            {
                c = a / b;
                textBox3.Text = c.ToString();
            }
            catch(DivideByZeroException ex)
            {
                MessageBox.Show(ex.Message,"Exception !");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();
            this.Hide();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
}

//Format Exception
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 Exceptions
{
    public partial class Form2 : Form
    {
        int a, b, c;
        public Form2()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                a = Int32.Parse(textBox1.Text);
                b = Int32.Parse(textBox2.Text);
            }
            catch (FormatException ex)
            {
                MessageBox.Show(ex.Message,"Exception !");
            }
            c = a + b;
            textBox3.Text = c.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3();
            f3.Show();
            this.Hide();
        }
        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
}


//Index Out of Range
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 Exceptions
{
    public partial class Form3 : Form
    {
        int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int i = Int32.Parse(textBox1.Text);
                label3.Text = array[i].ToString();
            }
            catch (IndexOutOfRangeException ex)
            {
                MessageBox.Show(ex.Message,"Exception !");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form4 f4 = new Form4();
            f4.Show();
            this.Hide();
        }

        private void Form3_Load(object sender, EventArgs e)
        {

        }
    }
}

//Overflow Exception
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 Exceptions
{
    public partial class Form4 : Form
    {
        Int16 a, b;
        int c;
        public Form4()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                a = Int16.Parse(textBox1.Text);
                b = Int16.Parse(textBox2.Text);
                c = a * b;
                textBox3.Text = c.ToString();
            }
            catch (OverflowException ex)
            {
                MessageBox.Show(ex.Message,"Exception !");
            }
        }

        private void Form4_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void Form4_Load(object sender, EventArgs e)
        {

        }
    }
}


Leave a Comment

* Email will not be published.
All comments are only visible after approval.
Most Viewed articles
Travel Blog - VR Indian Wanderers
VR Indian Wanderers
Govt. Jobs Portal
Govt. Jobs Portal
Free Chart Maker
Make free animated Charts from .CSV
Search
Youtube Channel
Podcast
Subscribe to Email Updates
Connect With Us
VR Indian Wanderers Youtube RSS Feed
© 2024 Learn with Vikas Suhag. All Rights Reserved.