Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
-C#- Error Handling
#1
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);
       }
[Image: bxj6gaq99c9hyvinfkvn.jpg]
[Image: w0l5y0.png]
#2
Why don't you simply think ahead and prevent the overflows and errors from ever having occurred to begin with?
Steam Wrote: 4:02 PM - George, of the jungle: was out
4:02 PM - George, of the jungle: bison, dude
4:02 PM - Brawl Bashin’ Bison: ???
4:02 PM - George, of the jungle: you're very rude towards alina
4:02 PM - George, of the jungle: how about unbanning her friend?
4:02 PM - George, of the jungle: I mean
4:02 PM - George, of the jungle: it's only gamebanana skins
4:02 PM - Brawl Bashin’ Bison: LOL
4:02 PM - George, of the jungle: ^^
4:02 PM - Brawl Bashin’ Bison: LOLOL
4:02 PM - George, of the jungle: lol
#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]
#4
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!
#5
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 }

Users browsing this thread: 1 Guest(s)