Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
i am working on a custom calculator app.
i am having trouble with:

C#
decimal tb1;
            decimal tb2;
            decimal answer;

            tb1 = decimal.Parse(textBox1.Text);
            tb2 = decimal.Parse(textBox2.Text);

            answer = tb1 / tb2;
            textBox1.Text = (answer.ToString());
            textBox2.Text = "0";

when textbox2 is 0 i would like a message to show up saying that dividing by zero is not possible, but i do not know you to use try and catch.
Posted
Comments
Toli Cuturicu 4-Mar-11 9:58am    
> i do not know you to use try and catch
Learn then! Read help (RTFM)

First, you can use TryParse instead of Parse.

There is nothing difficult in using try/catch. Moreover, programming without using of structured exception handling makes little sense.

Using exceptions with UI correctly is something special. Please see this:
How do i make a loop that will stop when a scrollbar reaches the bottom[^].

You can (and often) should parse exception locally as well. The pattern is very simple:

try {
    Evaluate(); //whatever you calculate
} catch (System.Format e) {
    ShowFormatProblem(); //select text box with problem so, show error message
} catch (System.ArithmeticException e) {
    ShowException(); //write some function to select textBox1 in question, show error message, etc.
}


Pay attention: it will only catch few exception classes: System.Format, System.Arithmetic exceptions and derived classes. All other exception will propagate top of the stack and will be caught in the level of Application (System.Windows.Forms or WPF, see the above reference).

It is very important not to block exception from propagation. Your case in one of the rare cases when you can block it for just a few exception classes. It it even much more rare case when you catch and block all exceptions silently; usually this is done to work around some software defect in third-party software not accessible for patching.

—SA
 
Share this answer
 
So the try block contains the guarded code that may cause the exception. The block is executed until an exception is thrown or it is completed successfully.

The catch clause can be used without arguments, in which case it catches any type of exception, and referred to as the general catch clause. It can also take an object argument derived from System.Exception, in which case it handles a specific exception.

decimal tb1;
decimal tb2;
decimal answer;
            try
            {
                tb1 = decimal.Parse(textBox1.Text);
                tb2 = decimal.Parse(textBox3.Text);
                answer = tb1 / tb2;
                textBox1.Text = String.Format("{0:0.0}", answer);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fail due to division by zero");
                string excpt = ex.InnerException.ToString();
            }


Here you will get an exception in answer = tb1 / tb2; so then it will directly go to catch block without processing the conversion of decimal to string.

But you in the code that I have provided you can get an exception while parsing the textbox values to decimal. There you have to check or avoid user input for letters.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 2-Mar-11 21:12pm    
Sorry, blocking all exceptions is bad! (Please see my answer with other answer, referenced.) Also, message box is pretty bad. (I would understand if this is just for example, but then mention it.) This time, my "3", sorry.
vlad781 2-Mar-11 21:14pm    
ok, thanks. this worked great. except for when i press the button, i get a failure at line:
string excpt = ex.InnerException.ToString();
when i take that out, it works fine without a problem.
rajivpande86 3-Mar-11 15:55pm    
what error you got??Can you post the message?
Toli Cuturicu 4-Mar-11 9:59am    
Well... I'm almost sure ex.InnerException was null.
vlad781 4-Mar-11 19:33pm    
hmm. interesting. the error isn't showing up, and it all works fine :)
If(tb2 != 0)
{
tb1 / tb2;
}

etc...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Mar-11 21:11pm    
No need to do that! It does not protect from other exception, so you need to do exception handling anyway (could be overflow and many more cases), so it is redundant, only the extra dirt in code.
--SA
Toli Cuturicu 4-Mar-11 10:01am    
Joking, huh?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900