What is the most recommended "try" or "if"

1
{   
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        double SalarioBase = 0, Descontos = 0, Vantagens = 0;
        try
        {
            SalarioBase = Convert.ToDouble(textBox1.Text);
            Vantagens = Convert.ToDouble(textBox2.Text);
            Descontos = Convert.ToDouble(textBox3.Text);
        }
        catch
        {
            MessageBox.Show("Valores Invalidos");
            return;
        }
        textBox4.Text = Convert.ToString (( SalarioBase + Vantagens ) - Descontos);
    }
}

What's the difference between using if and try

    
asked by anonymous 15.05.2017 / 00:19

3 answers

21
  

What's the difference between using if and try

Well, they are very different from each other. if is used to make a conditional flow. try ... catch is used to catch errors (or exceptions, in correct language) and give those errors the appropriate treatment.

For example, in your code:

try 
{
    SalarioBase = Convert.ToDouble(textBox1.Text);
    Vantagens = Convert.ToDouble(textBox2.Text);
    Descontos = Convert.ToDouble(textBox3.Text);
}
catch
{
    MessageBox("Valores inválidos");
    return;
}

If there is a conversion error in any of the three fields, your code will go to catch , that is, it will print an error message and exit the function.

About the title of your question:

  

What is the most recommended try or if

There is no such thing as "most commendable" in this case. Each serves a different purpose, so much that they can be used together. For example:

try 
{
    if (!Double.TryParse(textBox1.Text, out SalarioBase))
    {
        MessageBox("Salário não está no formato correto.");
    }

    if (!Double.TryParse(textBox2.Text, out Vantagens))
    {
        MessageBox("Vantagens não está no formato correto.");
    }

    if (!Double.TryParse(textBox3.Text, out Descontos))
    {
        MessageBox("Descontos não está no formato correto.");
    }
}
catch
{
    MessageBox("Ocorreu algum erro não previsto.");
    return;
}
    
15.05.2017 / 00:29
13

I would not use either, or even use if , but in a different way:

using static System.Console;

public class Program {
    public static void Main(string[] args) {
        var textBox1 = "123.45"; //"só para facilitar o teste
        var textBox2 = "10"; //"só para facilitar o teste
        var textBox3 = "abc"; //"só para facilitar o teste
        double SalarioBase;
        if (!double.TryParse(textBox1, out SalarioBase)) { 
            WriteLine("Salario Base foi digitado incorrentamente");
        }
        double Vantagens;
        if (!double.TryParse(textBox2, out Vantagens)) { 
            WriteLine("Vantagens foi digitado incorrentamente");
        }
        double Descontos;
        if (!double.TryParse(textBox3, out Descontos)) { 
            WriteLine("Descontos foi digitado incorrentamente");
        }
        WriteLine(SalarioBase);
        WriteLine(Vantagens);
    }
}

See at Coding Ground . Also should only be used in exceptional situations (including because is slow), which is not the case. Typed numbers may be wrong and it is quite normal that conversion is not possible. So I'd prefer prefers decimal that works correctly.

    
15.05.2017 / 00:39
-1

When you wait for an error, I always use If , as it generates a cleaner code.

Otherwise, choose try catch

    
15.05.2017 / 18:53