This code has several problems, some are errors, others is just bad style issue:
1 - This problem need not deal with exception. It needs to check the error and give due treatment. Use TryParse()
and be happy with no exceptions.
2 - If you are going to use exception, put in Finally
only what should be executed always. What should not be executed if generating an exception, put out.
3 - If you capture an exception, make it as specific as possible, just use Exception
if you have an excellent justification.
4 - If you will not use the exception variable, do not use it.
5 - Only declare the variable where you will use it, do not pre-declarations that do not bring benefits.
6 - It is not the end of the world in this case, but avoid concatenations of string 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...");
}
}
}
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
This code requires C# 7, but with a small modification can run in C# 6 and with little else can run in all versions.
If you still want to insist on the exception and even to answer the question, what you want is not to skip the finally
is that there is no finally
. The completion mechanism of the attempt is that the code must be executed in any case. If you want it to be skipped when you make the exception, then it should stay out of the whole block try-catch-finally
. Just like that. 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...");
}
}
}
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
Note that there is another exception occurring. I think it is something momentary in . NET Fiddle anyway shows how problematic it is to capture Exception
, take what you don’t expect.
I reinforce that all this is wrong, take the first.
If you want to finish a show and he’s not on Main()
then in place of return
should use the Environment.Exit()
, unless you are in a Winforms application, then you should use the Application.Exit()
. There are other variations for applications of other technologies (WPF, UWP, ASPNET, etc.), including if it is a test. There are cases that even other solutions are more suitable.
Besides the question doesn’t talk about finishing the application soon, it talks about skipping the catch
.
It doesn’t make much sense that you want it. The
finally
is to ensure that the routine within it is always executed if there is an exception and this exception stops the flow and causes it to exit the method where the exception occurred preventing an important code to be executed. With thefinally
you can give guarantees that it will be executed. If you want to run q on it, just don’t declare the blockfinally
and put your routine below thecatch
for its execution (without guarantees) .– gato