Using block and Exceptions

1

Is there any way to report errors in codes in block using as in try do we have catch ? Or using using I'm stuck with an infinity of if , else if and else ?

Assuming the using block forces you to create many conditions, which do you prefer? try, catch e finally or using, if, else if e else ?

    
asked by anonymous 22.05.2018 / 03:03

2 answers

4

The using is actually an implicit% of% and does not allow finally . Some even ask to have it, but I doubt I ever will. The solution when you need to catch an exception that can occur in a block that works with a resource that should be exempted at the end of its execution is to use a catch itself.

Just be careful because I see catching exceptions being abused, in many cases I would not need or even the exception should not be thrown.

Another common problem is that people do not quite understand what to do in try-catch-fianlly to call the finally method.

{
    var arq = new FileStream("arq.txt", FileMode.Create);
    try {
        //faz alguma coisa aqui
    } finally {
        if (arq != null) ((IDisposable)arq).Dispose();
    }
}

The Dispose() can be used in some contexts, depends on what you want to treat, if you use if properly then if will probably be used.

I gave a answer about the subject .

    
22.05.2018 / 03:14
2

Come on, let me explain.

using

This is when you will use an object and then discard it without storing information in memory.

using (StringBuilder sb = new StringBuilder()) {
    sb.AppendLine("Olá, mundo!");
}
// o StringBuilder sb não existe mais na memória

It is only used with objects that implement IDisposeable (disposable object). The using block does not implement any condition or exception expressions.

try

is used when you will catch runtime errors within a block and will handle them.

int valor = 0;
try {
    // tente fazer isso
    valor = Convert.ToInt32("Isso não é um número");
} catch (Exception ex) {
    // se der erro, faça isso com o erro:
    Console.WriteLine("Não deu certo, pois " + ex.Message);
    valor = 10;
} finally {
    // no final, mesmo se der certo ou der erro,
    valor *= 2;
}

At the end, value will be 20 , because inside the block it will give an error to convert that string to a number. In block catch , value 10 is assigned to value , and in block finally , the value is multiplied by two.

It is not recommended to use try , but only when you will do something that needs user manipulation.

if

This is the classic conditional, it will only execute what is inside the block if the expression is true :

if(1 + 1 == 2) {
     Console.WriteLine("Isso irá executar");
}
if(2 + 2 == 5) {
     Console.WriteLine("Isso não irá executar");
}

Of course obvious and obvious values were used in the example above, but the if block accepts variables and constants, as long as the final value is Boolean .

Where to use it all?

You can combine the three blocks above, one inside the other, each one performing a function. You just have to understand what each one does.

Read the documentation .

    
22.05.2018 / 05:34