This code has several problems, some are errors, others are just a bad style question:
1 - This problem does not have to deal with exception. He needs to check the error and give due treatment. Use TryParse()
and be happy without exceptions.
2 - If it were to use exception, put in Finally
only what should always be executed. What should not be run if you raise an exception, put it away.
3 - If you catch an exception, do as specific as possible, only use Exception
if you have a good justification.
4 - If you are not going to use the exception variable, do not use it.
5 - Just declare the variable where you are going to use it, do not make pre-statements that do not bring benefits.
6 - It is not the end of the world in this case, but avoid string concatenations and prefer interpolation of string .
7 - I made other almost cosmetic improvements.
using static System.Console;
namespace Calculadora {
public class Program {
public static void Main(string[] args) {
WriteLine("Digite o primeiro numero.");
if (!double.TryParse(ReadLine(), out var n1)) { //dá para fazer genérico
WriteLine("Dado digitado inválido");
return;
}
WriteLine("Digite o segundo numero.");
if (!double.TryParse(ReadLine(), out var n2)) { //reaproveite e não é necessário
WriteLine("Dado digitado inválido");
return;
}
double multiplicacao = n1 * n2;
WriteLine("Resultado...");
WriteLine($"{n1} * {n2} = {multiplicacao}");
WriteLine("Tudo foi executado...");
}
}
}
See running on .NET Fiddle .
This code requires C # 7, but with a little modification it can run in C # 6 and with little else it can run in all versions.
If you still want to insist on the exception and even to answer the question, you do not want to skip finally
and there is no finally
. The attempt completion mechanism is that the code should be executed in any case. If you want it to be skipped when you give the exception, then it should be outside of the try-catch-finally
block. That simple. You can do:
using System;
using static System.Console;
namespace Calculadora {
public class Program {
public static void Main(string[] args) {
try {
WriteLine("Digite o primeiro numero.");
double n1 = double.Parse(ReadLine());
WriteLine("Digite o segundo numero.");
double n2 = double.Parse(ReadLine());
double multiplicacao = n1 * n2;
WriteLine("Resultado...");
WriteLine($"{n1} * {n2} = {multiplicacao}");
} catch (Exception) { //só para efeitos de teste, caso contrário não capture Exception
WriteLine("Erro ao tentar fazer a conta."); //na prática agora nunca acontecerá a exceção
WriteLine("O finally nao foi executou.");
return;
}
WriteLine("Tudo foi executado...");
}
}
}
See running on .NET Fiddle . And no Coding Ground . Also put it in GitHub for future reference .
Note that there is another exception occurring. I think it's something momentary in .NET Fiddle, anyway it shows how problematic it is to catch Exception
, get what you do not expect.
I stress that all of this is wrong, stay with the first one.
If you want to terminate a program and it is not in Main()
then in place of return
you must use the Environment.Exit()
", unless you are in a WinForms application, you should use Application.Exit()
. There are other variations for applications of other technologies (WPF, UWP, ASPNET, etc.), even if it is a test. There are cases that even other solutions are more appropriate.
In addition to the fact that the question does not say when to finish the application, it talks about skipping catch
.