![]() |
|
-C#- Error Handling - Printable Version +- (wL) Forums (https://war-lords.net/forum) +-- Forum: Resources (https://war-lords.net/forum/forum-31.html) +--- Forum: Programming (https://war-lords.net/forum/forum-33.html) +--- Thread: -C#- Error Handling (/thread-5047.html) |
-C#- Error Handling - MindHACKer - Nov 21 2011 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);
}RE: -C#- Error Handling - M. Bison - Nov 21 2011 Why don't you simply think ahead and prevent the overflows and errors from ever having occurred to begin with? RE: -C#- Error Handling - MindHACKer - Nov 22 2011 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!");
}
}
}& keeps me from being embarrassed in front of the Prof when i show her the Project. ^_^ RE: -C#- Error Handling - Helices - Nov 29 2011 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! RE: -C#- Error Handling - Leaky - Nov 30 2011 Helices Wrote:To farther add onto MindHackers post, don't forget you can re-throw exceptions through catch: 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 } |