1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: C# Dividebyzero exception

Discussion in 'Software Development' started by supergoten, Apr 30, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. supergoten

    supergoten Thread Starter

    Joined:
    Apr 14, 2009
    Messages:
    28
    I have this calculator program working, but how do I make the dividebyzero exception work right?

    Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    namespace Calculator
    {
        public class Form1 : System.Windows.Forms.Form
        {
            private System.Windows.Forms.Button btn4;
            private System.Windows.Forms.Button btn7;
            private System.Windows.Forms.Button btn8;
            private System.Windows.Forms.Button btn9;
            private System.Windows.Forms.Button btn1;
            private System.Windows.Forms.Button btn2;
            private System.Windows.Forms.Button btn3;
            private System.Windows.Forms.Button btn5;
            private System.Windows.Forms.Button btn6;
            private System.Windows.Forms.Button btn0;
            private System.Windows.Forms.Button btndivide;
            private System.Windows.Forms.Button btntimes;
            private System.Windows.Forms.Button btnminus;
            private System.Windows.Forms.Button btnplus;
            private System.Windows.Forms.Button btnclear;
            private System.Windows.Forms.Button btnpoint;
            private System.Windows.Forms.Button btnequal;
            private string answer;
            private ArrayList store;
            private System.Windows.Forms.Label lblanswer;
    
    
            private System.ComponentModel.Container components = null;
    
            public Form1()
            {
                InitializeComponent();
                answer = "";
                store = new ArrayList();
            }
            private void AddOperatorToArray(Button btn)
            {
                store.Add( answer )    ;
                lblanswer.Text    += btn.Text;
                answer= "";
                store.Add( btn.Text );
                btnpoint.Enabled = true;
                SetEnableOperatorbtns(false);
            }
            private void AddToArray(Button btn)
            {
                answer += btn.Text    ;
                lblanswer.Text  += btn.Text;
                SetEnableOperatorbtns(true);
            }
    
            private void Reset()
            {
                answer =    ""    ;
                lblanswer.Text    = "";
                store.Clear();
                btnpoint.Enabled = true;
            }
            
            private void SetEnableOperatorbtns(bool enable)
            {
                btnplus.Enabled= enable;
                btnminus.Enabled= enable    ;
                btntimes.Enabled    = enable    ;
                btndivide.Enabled= enable    ;
                btnequal.Enabled    = enable    ;
            }
            protected override void Dispose( bool disposing )
            {
                if( disposing )
                {
                    if (components != null) 
                    {
                        components.Dispose();
                    }
                }
                base.Dispose( disposing );
            }
    
            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.btn1 = new System.Windows.Forms.Button();
                this.btn2 = new System.Windows.Forms.Button();
                this.btn3 = new System.Windows.Forms.Button();
                this.btn4 = new System.Windows.Forms.Button();
                this.btn5 = new System.Windows.Forms.Button();
                this.btn6 = new System.Windows.Forms.Button();
                this.btn7 = new System.Windows.Forms.Button();
                this.btn8 = new System.Windows.Forms.Button();
                this.btn9 = new System.Windows.Forms.Button();
                this.btn0 = new System.Windows.Forms.Button();
                this.btndivide = new System.Windows.Forms.Button();
                this.btntimes = new System.Windows.Forms.Button();
                this.btnminus = new System.Windows.Forms.Button();
                this.btnplus = new System.Windows.Forms.Button();
                this.btnclear = new System.Windows.Forms.Button();
                this.btnpoint = new System.Windows.Forms.Button();
                this.btnequal = new System.Windows.Forms.Button();
                this.lblanswer = new System.Windows.Forms.Label();
                this.SuspendLayout();
                // 
                // btn1
                // 
                this.btn1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btn1.Location = new System.Drawing.Point(32, 184);
                this.btn1.Name = "btn1";
                this.btn1.Size = new System.Drawing.Size(48, 32);
                this.btn1.TabIndex = 1;
                this.btn1.Text = "1";
                this.btn1.Click += new System.EventHandler(this.btnD1_Click);
                // 
                // btn2
                // 
                this.btn2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btn2.Location = new System.Drawing.Point(88, 184);
                this.btn2.Name = "btn2";
                this.btn2.Size = new System.Drawing.Size(48, 32);
                this.btn2.TabIndex = 2;
                this.btn2.Text = "2";
                this.btn2.Click += new System.EventHandler(this.btnD2_Click);
                // 
                // btn3
                // 
                this.btn3.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btn3.Location = new System.Drawing.Point(144, 184);
                this.btn3.Name = "btn3";
                this.btn3.Size = new System.Drawing.Size(48, 32);
                this.btn3.TabIndex = 3;
                this.btn3.Text = "3";
                this.btn3.Click += new System.EventHandler(this.btnD3_Click);
                // 
                // btn4
                // 
                this.btn4.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btn4.Location = new System.Drawing.Point(32, 144);
                this.btn4.Name = "btn4";
                this.btn4.Size = new System.Drawing.Size(48, 32);
                this.btn4.TabIndex = 4;
                this.btn4.Text = "4";
                this.btn4.Click += new System.EventHandler(this.btnD4_Click);
                // 
                // btn5
                // 
                this.btn5.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btn5.Location = new System.Drawing.Point(88, 144);
                this.btn5.Name = "btn5";
                this.btn5.Size = new System.Drawing.Size(48, 32);
                this.btn5.TabIndex = 5;
                this.btn5.Text = "5";
                this.btn5.Click += new System.EventHandler(this.btnD5_Click);
                // 
                // btn6
                // 
                this.btn6.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btn6.Location = new System.Drawing.Point(144, 144);
                this.btn6.Name = "btn6";
                this.btn6.Size = new System.Drawing.Size(48, 32);
                this.btn6.TabIndex = 6;
                this.btn6.Text = "6";
                this.btn6.Click += new System.EventHandler(this.btnD6_Click);
                // 
                // btn7
                // 
                this.btn7.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btn7.Location = new System.Drawing.Point(32, 104);
                this.btn7.Name = "btn7";
                this.btn7.Size = new System.Drawing.Size(48, 32);
                this.btn7.TabIndex = 7;
                this.btn7.Text = "7";
                this.btn7.Click += new System.EventHandler(this.btnD7_Click);
                // 
                // btn8
                // 
                this.btn8.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btn8.Location = new System.Drawing.Point(88, 104);
                this.btn8.Name = "btn8";
                this.btn8.Size = new System.Drawing.Size(48, 32);
                this.btn8.TabIndex = 8;
                this.btn8.Text = "8";
                this.btn8.Click += new System.EventHandler(this.btnD8_Click);
                // 
                // btn9
                // 
                this.btn9.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btn9.Location = new System.Drawing.Point(144, 104);
                this.btn9.Name = "btn9";
                this.btn9.Size = new System.Drawing.Size(48, 32);
                this.btn9.TabIndex = 9;
                this.btn9.Text = "9";
                this.btn9.Click += new System.EventHandler(this.btnD9_Click);
                // 
                // btn0
                // 
                this.btn0.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btn0.Location = new System.Drawing.Point(32, 224);
                this.btn0.Name = "btn0";
                this.btn0.Size = new System.Drawing.Size(48, 32);
                this.btn0.TabIndex = 10;
                this.btn0.Text = "0";
                this.btn0.Click += new System.EventHandler(this.btnD0_Click);
                // 
                // btndivide
                // 
                this.btndivide.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btndivide.Location = new System.Drawing.Point(216, 176);
                this.btndivide.Name = "btndivide";
                this.btndivide.Size = new System.Drawing.Size(48, 24);
                this.btndivide.TabIndex = 11;
                this.btndivide.Text = "/";
                this.btndivide.Click += new System.EventHandler(this.btnDiv_Click);
                // 
                // btntimes
                // 
                this.btntimes.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btntimes.Location = new System.Drawing.Point(216, 152);
                this.btntimes.Name = "btntimes";
                this.btntimes.Size = new System.Drawing.Size(48, 24);
                this.btntimes.TabIndex = 12;
                this.btntimes.Text = "*";
                this.btntimes.Click += new System.EventHandler(this.btnMul_Click);
                // 
                // btnminus
                // 
                this.btnminus.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btnminus.Location = new System.Drawing.Point(216, 128);
                this.btnminus.Name = "btnminus";
                this.btnminus.Size = new System.Drawing.Size(48, 24);
                this.btnminus.TabIndex = 13;
                this.btnminus.Text = "-";
                this.btnminus.Click += new System.EventHandler(this.btnMinus_Click);
                // 
                // btnplus
                // 
                this.btnplus.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btnplus.Location = new System.Drawing.Point(216, 104);
                this.btnplus.Name = "btnplus";
                this.btnplus.Size = new System.Drawing.Size(48, 24);
                this.btnplus.TabIndex = 14;
                this.btnplus.Text = "+";
                this.btnplus.Click += new System.EventHandler(this.btnPlus_Click);
                // 
                // btnclear
                // 
                this.btnclear.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btnclear.Location = new System.Drawing.Point(144, 224);
                this.btnclear.Name = "btnclear";
                this.btnclear.Size = new System.Drawing.Size(66, 32);
                this.btnclear.TabIndex = 15;
                this.btnclear.Text = "Clear";
                this.btnclear.Click += new System.EventHandler(this.btnClr_Click);
                // 
                // btnpoint
                // 
                this.btnpoint.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btnpoint.Location = new System.Drawing.Point(88, 224);
                this.btnpoint.Name = "btnpoint";
                this.btnpoint.Size = new System.Drawing.Size(48, 32);
                this.btnpoint.TabIndex = 16;
                this.btnpoint.Text = ".";
                this.btnpoint.Click += new System.EventHandler(this.btnDot_Click);
                // 
                // btnequal
                // 
                this.btnequal.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btnequal.Location = new System.Drawing.Point(216, 200);
                this.btnequal.Name = "btnequal";
                this.btnequal.Size = new System.Drawing.Size(48, 24);
                this.btnequal.TabIndex = 17;
                this.btnequal.Text = "=";
                this.btnequal.Click += new System.EventHandler(this.btnEqual_Click);
                // 
                // lblanswer
                // 
                this.lblanswer.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.lblanswer.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.lblanswer.Location = new System.Drawing.Point(32, 40);
                this.lblanswer.Name = "lblanswer";
                this.lblanswer.Size = new System.Drawing.Size(232, 32);
                this.lblanswer.TabIndex = 18;
                this.lblanswer.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
                // 
                // Form1
                // 
                this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                this.BackColor = System.Drawing.Color.Plum;
                this.ClientSize = new System.Drawing.Size(292, 294);
                this.Controls.Add(this.lblanswer);
                this.Controls.Add(this.btnequal);
                this.Controls.Add(this.btnpoint);
                this.Controls.Add(this.btnclear);
                this.Controls.Add(this.btnplus);
                this.Controls.Add(this.btnminus);
                this.Controls.Add(this.btntimes);
                this.Controls.Add(this.btndivide);
                this.Controls.Add(this.btn0);
                this.Controls.Add(this.btn9);
                this.Controls.Add(this.btn8);
                this.Controls.Add(this.btn7);
                this.Controls.Add(this.btn6);
                this.Controls.Add(this.btn5);
                this.Controls.Add(this.btn4);
                this.Controls.Add(this.btn3);
                this.Controls.Add(this.btn2);
                this.Controls.Add(this.btn1);
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                this.Name = "Form1";
                this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                this.Text = "Calculator";
                this.Load += new System.EventHandler(this.Form1_Load);
                this.ResumeLayout(false);
    
            }
            #endregion
    
            [STAThread]
            static void Main() 
            {
                Application.Run(new Form1());
            }
    
            private void btnD0_Click(object sender, System.EventArgs e)
            {
                AddToArray(btn0);    
            }
    
            private void btnD1_Click(object sender, System.EventArgs e)
            {
                AddToArray(btn1);    
            }
    
            private void btnD2_Click(object sender, System.EventArgs e)
            {
                AddToArray(btn2);    
            }
    
            private void btnD3_Click(object sender, System.EventArgs e)
            {
                AddToArray(btn3);    
            }
    
            private void btnD4_Click(object sender, System.EventArgs e)
            {
                AddToArray(btn4);    
            }
    
            private void btnD5_Click(object sender, System.EventArgs e)
            {
                AddToArray(btn5);    
            }
    
            private void btnD6_Click(object sender, System.EventArgs e)
            {
                AddToArray(btn6);    
            }
    
            private void btnD7_Click(object sender, System.EventArgs e)
            {
                AddToArray(btn7);    
            }
    
            private void btnD8_Click(object sender, System.EventArgs e)
            {
                AddToArray(btn8);    
            }
    
            private void btnD9_Click(object sender, System.EventArgs e)
            {
                AddToArray(btn9);    
            }
    
            private void btnDiv_Click(object sender, System.EventArgs e)
            {
                AddOperatorToArray(btndivide);
            }
    
            private void btnMul_Click(object sender, System.EventArgs e)
            {
                AddOperatorToArray(btntimes);
            }
    
            private void btnMinus_Click(object sender, System.EventArgs e)
            {
                AddOperatorToArray(btnminus);
            }
    
            private void btnPlus_Click(object sender, System.EventArgs e)
            {
                AddOperatorToArray(btnplus);
            }
    
            private void btnDot_Click(object sender, System.EventArgs e)
            {
                AddToArray(btnpoint)            ;
                btnpoint.Enabled = false        ;
            }
    
            private void btnClr_Click(object sender, System.EventArgs e)
            {
                Reset();
            }
    
            private void btnEqual_Click(object sender, System.EventArgs e)
            {
                try
                {
                    store.Add(answer);
                    store.Add( btnequal.Text )    ;
        
                    float result = float.Parse ( store[0].ToString() ) ;
                    
                    for( int i = 0 ;  i<store.Count ; i++ )
                    {
                    
                        if( store[i].ToString() == "+" )
                        {
                            lblanswer.Text    ="";
                            result += float.Parse (store[i+1].ToString());
                            lblanswer.Text = result.ToString();        
                        }
                        else if( store[i].ToString() == "-" )
                        {
                            lblanswer.Text="";
                            result -= float.Parse (store[i+1].ToString())    ;
                            lblanswer.Text= result.ToString();        
                        }
                        else if( store[i].ToString() == "*" )
                        {
                            lblanswer.Text    ="";
                            result *= float.Parse ( store[i+1].ToString() );
                            lblanswer.Text= result.ToString()    ;        
                        }
                        else if( store[i].ToString() == "/" )
                        {
                            lblanswer.Text="";
                            result /= float.Parse ( store[i+1].ToString() );
                            lblanswer.Text= result.ToString();        
                        }
                    }
                    store.Clear();
                    answer = lblanswer.Text;
    
                    for( int i = 0 ; i < answer.Length ; i++ )
                    {
                        if( answer[i].ToString() == "." )
                        {
                            btnpoint.Enabled = false    ;
                            break;
                        }
                        else
                        {
                            btnpoint.Enabled = true;
                        }
                    }
                    
                    btnequal.Enabled = false;
                }
                catch(System.DivideByZeroException ex)
                {
                    MessageBox.Show(ex.Message,"Can not divide by zero!");
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
        }
    }
    
     
  2. supergoten

    supergoten Thread Starter

    Joined:
    Apr 14, 2009
    Messages:
    28
    Anyone? I know it's a long code but I just need a little help.
     
  3. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    Code:
    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 techguyhelpcsharp
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                int a;
                int b;
                int c;
                a = 3;
                b = 0;
                try
                {
                    c = a / b;
                }
                catch (System.DivideByZeroException e)
                {
                    MessageBox.Show(e.Message, "CAN'T DIVIDE BY ZERO!");
                }
            }
        }
    }
    
    
    The simple example above works, so it mustn't be throwing the exception in the first place. Is any error being thrown?

    You could include the following check:
    Code:
     else if( store[i].ToString() == "/" )
                        {
                            lblanswer.Text="";
                            if(store[i+1].ToString() == 0)
                            {
                                   throw new System.DivideByZeroException("Can't divide by zero!");
                            }
                            result /= float.Parse ( store[i+1].ToString() );
                            lblanswer.Text= result.ToString();        
                        }
    
    This should trigger the exception

    Hope that does it,
    Andy
     
  4. supergoten

    supergoten Thread Starter

    Joined:
    Apr 14, 2009
    Messages:
    28
    Thank you, I solved it!
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/920312

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice