Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,080 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,584 people online right now. Registration is fast and FREE... Join Now!




where should I put a try/catch block?

 
Reply to this topicStart new topic

where should I put a try/catch block?

OliveOyl3471
12 Apr, 2008 - 09:44 AM
Post #1

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,611



Thanked: 17 times
Dream Kudos: 150
My Contributions
Where is the proper place to put a try/catch block in my program? I need to catch the exception that occurs when the user leaves a textbox blank. (input not in correct format) I want to do this:
CODE

try
{
//my code here;
if(my condition here)
{
//do something;
}
else
//do something else;
}
catch (Exception ex)
{ messageBox.Show("my error message here");
}


Is this done right? Can you have an if statement within a try catch block? It gives me errors when I try it.
Also, should I use methods to perform the calculations or is it proper as is? Could that be why I am getting the errors when I use the try/catch block, because I don't use methods, and the variables can't be accessed?

This is just a rough first draft...it does work without the try/catch, but I think it's kind of sloppy, and it doesn't account for all exceptions.

CODE


private void button1_Click(object sender, EventArgs e)
        {
            double miles, km, kg, pounds, miles2, knots, milesAns, kmAns, kgAns, poundsAns, miles2Ans, knotsAns;
          
          
            //convert user input to numeric
                miles = int.Parse(textBox1.Text);
                km = int.Parse(textBox2.Text);
                kg = int.Parse(textBox3.Text);
                pounds = int.Parse(textBox4.Text);
                miles2 = int.Parse(textBox5.Text);
                knots = int.Parse(textBox6.Text);
          
                //do not accept zeros
if (miles == 0 || km == 0 || kg == 0 || pounds == 0 || miles2 == 0 || knots == 0)
{
     MessageBox.Show("please do not enter any zeros");
     textBox1.Text = "";
     textBox2.Text = "";
     textBox3.Text = "";
     textBox4.Text = "";
     textBox5.Text = "";
     textBox6.Text = "";
}
else
{
     //perform calculations
     //1 Mile = 1.60934 Kilometers
     kmAns = miles * 1.60934;
     //1 Kilometer = 0.62137 Miles
     milesAns = km * .62137;
     //1 Pound = 0.45359 Kilograms;
     kgAns = pounds * .45359;
     //1 Kilogram = 2.20462 * Pounds;
     poundsAns = kg * 2.20462;
     //1 Miles Per Hour = 0.86898 Knot
     knotsAns = miles2 * .86898;
     //1.00000 Knot = 1.15078 Miles Per Hour
     miles2Ans = knots * 1.15078;

     //display results
     textBox7.Text = kmAns.ToString();
     textBox8.Text = milesAns.ToString();
     textBox9.Text = kgAns.ToString();
     textBox10.Text = poundsAns.ToString();
     textBox11.Text = knotsAns.ToString();
     textBox12.Text = miles2Ans.ToString();
}


User is offlineProfile CardPM
+Quote Post

baavgai
RE: Where Should I Put A Try/catch Block?
12 Apr, 2008 - 10:06 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Why are you parsing for int and storing in double... Also, more descriptive field names would be nice.

I'd make a value checker / grabber method. Also, I wouldn't clear all the values if one is zero. Additionally, it's nice to focus on the offending field and explain to the user what much be changed.

csharp

private int GetValueIntNoZero(TextBox ctl, string controlName) {
int value = 0;
try {
value = int.Parse(ctl.Text);
} catch (Exception ex) {
}
if (value==0) {
ctl.Text = "";
ctl.Focus()
throw new Exception(controlName + "must be numeric and non zero.");
}
return value;
}


private void button1_Click(object sender, EventArgs e) {
double miles, km, kg, pounds, miles2, knots, milesAns, kmAns, kgAns, poundsAns, miles2Ans, knotsAns;

try {
miles = GetValueIntNoZero(textBox1, "miles");
km = GetValueIntNoZero(textBox2, "kg");
//...
} catch (Exception ex) {
MessageBox.Show(ex.Message);
return;
}
//...


Hope this helps.

User is offlineProfile CardPM
+Quote Post

OliveOyl3471
RE: Where Should I Put A Try/catch Block?
12 Apr, 2008 - 03:30 PM
Post #3

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,611



Thanked: 17 times
Dream Kudos: 150
My Contributions
Thank you. You've made some good suggestions. I will try to do as you said.

Parsing for int and storing in double...because when I stored in int, it didn't work...I guess the values were out of range for an int.
Is there a double.Parse() method available?


User is offlineProfile CardPM
+Quote Post

baavgai
RE: Where Should I Put A Try/catch Block?
12 Apr, 2008 - 04:11 PM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
QUOTE(OliveOyl3471 @ 12 Apr, 2008 - 07:30 PM) *

Is there a double.Parse() method available?


Yes, there is. It can also be accessed through the Convert class method, Convert.ToDecimal(value).

The reason you probably had issues with int is implicit conversion type rules. i.e. int * double will always result in int. The solution is to cast the int as a double. It's a little gotcha that actually exists in most languages.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 07:39PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month