Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
-C#- Error Handling
#3
M. Bison Wrote:Why don't you simply think ahead and prevent the overflows and errors from ever having occurred to begin with?
I do, but i use this Method to keep my program from crashing, in case i forgot something, or if there was a missing link or file (It's very useful).
I'll give you an example:
this is a simple calculator code free of errors or overflows, before running..
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 hhhhh
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            try
                    {
                        int z;
                        char t = Convert.ToChar(textBox2.Text);

                        switch (t)
                        {
                            case '+':
                                z = Convert.ToInt32(textBox1.Text != null) + Convert.ToInt32(textBox3.Text != null);
                                textBox4.Text = z.ToString();
                                break;
                            case '-':
                                z = Convert.ToInt32(textBox1.Text) - Convert.ToInt32(textBox3.Text);
                                textBox4.Text = z.ToString();
                                break;
                            case '*':
                                z = Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox3.Text);
                                textBox4.Text = z.ToString();
                                break;
                            case '/':
                                float doc = Convert.ToSingle(textBox1.Text) / Convert.ToSingle(textBox3.Text);
                                textBox4.Text = doc.ToString();
                                break;
                        }
                    }
                    catch (Exception ex)
                    {
                       
                        MessageBox.Show(ex.Message);
                    }


                   
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("NeptuneHACK!");
        }

     }
}
As you can see No errors, now what if the user left the textboxs empty & pressed Button1, the program will crash... so the Method explains the Error & keeps the program from crashing.
& keeps me from being embarrassed in front of the Prof when i show her the Project.  ^_^
[Image: bxj6gaq99c9hyvinfkvn.jpg]
[Image: w0l5y0.png]

Messages In This Thread
-C#- Error Handling - by MindHACKer - Nov 21 2011, 01:15 PM
RE: -C#- Error Handling - by M. Bison - Nov 21 2011, 10:34 PM
RE: -C#- Error Handling - by MindHACKer - Nov 22 2011, 05:31 AM
RE: -C#- Error Handling - by Helices - Nov 29 2011, 05:43 AM
RE: -C#- Error Handling - by Leaky - Nov 30 2011, 05:04 PM

Users browsing this thread: 1 Guest(s)