(wL) Forums

Full Version: -C#- Error Handling
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
If we want to make our program more powerful and stylish, we need to stop it from crashing on Overflows & Errors, so we use this code to prevent it from crashing, & instead reporting the Error or the cause..

We use these two Methods like this:

Code:
try
   {
      //What you want...
   }
catch (Exception ex)
       {
            Messagebox.show (ex.message);
       }
Why don't you simply think ahead and prevent the overflows and errors from ever having occurred to begin with?
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.  ^_^
To farther add onto MindHackers post, don't forget you can re-throw exceptions through catch:

Code:
catch (Exception ex)
{
    throw (ex);   
}

Also, don't forget it is also possible to use more than one catch clause with a try-catch statement! (right out of MSDN). There is no limitation there!
Helices Wrote:To farther add onto MindHackers post, don't forget you can re-throw exceptions through catch:

Code:
catch (Exception ex)
{
    throw (ex);   
}

Also, don't forget it is also possible to use more than one catch clause with a try-catch statement! (right out of MSDN). There is no limitation there!


you would use multiple catch clauses to catch different types of exceptions.


Code:
try { }
catch (exception1 ex) { }
catch (exception2 ex) { }
catch (Exception ex) { this is your catch all exception incase the exception doesn't match the previous ones }
finally { this executes after the try/catch block, no matter what }