Thursday, November 6, 2014

Exception Handling Class



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace ExceptionHandling
{
    class OddNumberException : Exception
    {
        public OddNumberException() : base("odd number is an invalid input") { }
        public OddNumberException(string message) : base(message) { }
        public OddNumberException(string message, Exception innerException) : base(message, innerException) { }
    }
}
//====================================================================
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 ExceptionHandling
{
    public partial class Form1 : Form
    {
        /*
         MSDN: Exceptions are errors that occur during application execution
       
         The class Exception is the base class for all exceptions.
         When an error occurs, the operating system, or your running application reports it
         by throwing an exception. The Exception defines a property Message, that you could
         use to get extra information about the error.
         */
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCatchExceptions_Click(object sender, EventArgs e)
        {
            try
            {
                int v1 = int.Parse(txtValue1.Text);
                if ((v1 & 1) == 0)
                    throw new EvenNumberException("Even number is not accepted");

                int v2 = int.Parse(txtValue2.Text);


                int q = v1 / v2; //try entering 0 for v2. The exception will be caught
                //by ArithmeticException since it is the parent of
                //  DividebyZeroException.
                //create an exception for even number
                //if v1 is even we want to throw an exception\

                //call a method that throws an exception
                int result = MySqrt(v2);//input an odd number for v2 to test the
                //exception thrown by MySqrt method
            }
            catch (OddNumberException one)
            {
                MessageBox.Show("OddNumber Exception: \n" + one.Message);
            }
            catch (FormatException fe)
            {
                MessageBox.Show("Format Exception: \n" + fe.Message);
            }
            catch (OverflowException oe)
            {
                MessageBox.Show("Overflow Exception: \n" + oe.Message);
            }
            catch (EvenNumberException ene)
            {
                MessageBox.Show("EvenNumber Exception: \n" + ene.Message);
            }
            catch (ArithmeticException ae)
            {
                MessageBox.Show("Arithmetic Exception: \n" + ae.Message);
            }
           
        }

        private int MySqrt(int n)
        {
            //assume that this method only accepts even numbers
            //that is it reject odd numbers
            //this method should throw a custom exception when it encounters
            //an odd number

            //define a custome exception
            //and throw it
            if ((n & 1) != 0)
                throw new OddNumberException("odd numbers are not accepted");

            return (int)Math.Sqrt(n);
        }
    }
}
///lab assignment:
///New project
///Define a class WaterTank with the following fields:
///                                     _maxCapacity
///                                     _currentAmount
///                                     _minCapacity
///These fields hold the amount in gallons
///the _maxCapacity and _minCapacity should be static and set to some values
///
///Define a method :AddWater that takes the amount to be added to the tank
///this method should throw a custom exception if you attemp to add more water than 
///its max capacity
///
///Define a method: TakeWater that is passed the amount of water taken from the tank
///this method shoud throw a custome exception if you the current level drops may drop
///below mincapacity.
///
///In Form1 create a WaterTank object, then get amount to add to or take from the tank
///watch for the exception thrown.

No comments:

Post a Comment