Finally, a block in C # should?

What is the difference between the two conditions? Each time method1 or method2 is executed, there must be a block of code that needs to be executed. It seems to me that the 2 methods are the same.

// example method1 void Method1(void) { try { // do something } catch (Exception ex) { // do something } finally { // do something whenever method1 runs } } // example method2 void Method2(void) { try { // do something } catch (Exception ex) { // do something } // do something whenever method2 runs } 

Finally, the block seems unnecessary to me.

+4
source share
7 answers

In the first example, you can throw an exception again, and the code inside finally will still execute. This would be impossible in the second example.

If you decide not to throw the exception, then yes, there is a slight difference. However, this is considered a bad form - very rarely you need to use an exception that cannot be explicitly handled .

This is a keyword that will help you with the flow of code. When you throw an exception, it affects the flow of code (for example, using return ), the finally keyword allows you to express that when an exception occurs (or you return from try ) you still need execution to do something, because it leaves.

To answer the question annoyingly, it is necessary when you need it, and not when you do not.


additional literature

To be safe, before you try to start using this keyword, read it:

http://msdn.microsoft.com/en-us/library/zwc8s4fz.aspx

And the keywords for exception handling in general:

http://msdn.microsoft.com/en-us/library/s7fekhdy.aspx


<Strong> Examples

Catch an exception to do something with it, and then re-throw it. Use finally to invoke any code:

 try { OpenConnectionToDatabase(); // something likely to fail } catch (Exception ex) { Log(ex); throw; // throw ex; // also works but behaves differently } // Not specifying an exception parameter also works, but you don't get exception details. //catch (Exception) //{ // Log("Something went wrong); // throw; //} finally { CloseConnectionToDatabase(); } 

Do not register interest in catching exceptions, but use finally to organize the code:

 try { OpenConnectionToDatabase(); // something likely to fail } finally { CloseConnectionToDatabase(); } 

Come back from your try because it looks well formatted, but still use finally to arrange the code:

 try { OpenConnectionToDatabase(); return 42; } finally { CloseConnectionToDatabase(); } 
+15
source

As you know, code written inside a finally block is always executed. Please take a look at the next paragraph, written below, it will clear your confusion.

  • Finally, Resource Management is used . Mainly to free up resources . It always works regardless of the Exception.
    • As you know, catch is used to handle an exception, but sometimes it cannot handle an external exception . The finally block is then used to handle this exception to complete the operation.
+3
source

The code in the finally block will work anyway after try-catch, it is very useful for cleaning.

 try { // open resources } catch (Exception ex) { // something bad happened } finally { // close resources that are still opened } 
+2
source

This will behave differently depending on whether you use return from try . Also, finally will be executed even if catch throws an exception (or re-throws the original exception), which will not be executed without finally .

So: this is not required , but will behave differently. Therefore, if you want the code to happen, put it in finally .

In many ways, try / finally much more common than try / catch or try / catch / finally .

+1
source

You don’t have to have a finally block, but it ensures that the code inside it will always be executed (unless there is an exception at the end!).

Consider the following:

 void Method2(void) { try { // do something } catch (Exception ex) { // do something throw; } // do something whenever method2 runs } 

the code after try/catch will not be executed if an exception is thrown. In addition, if the code in the catch has an error that throws an exception (for example, when starting a log that caused an unexpected exception), the code that was supposed to be in finally will not be executed, deletion and cleaning are canceled.

Also, the return will lead to the fact that this code will not be run, while it will finally be executed anyway (also here you can see that the trick can also be skipped, which allows you to exclude any exceptions for propagating upwards - AFTER finally ) :

 void Method2(void) { try { // do something return } finally { // do something whenever method2 runs } } 

If you have cleanup code that should be run at the end of the method, use finally (or if your objects implement IDisposable , use the using statement).

+1
source

The finally block ensures that any code inside it will ALWAYS be executed, so if you have a return statement inside the try block or rebuild the exception in your catch block, the code inside the finally block will always execute.

This is necessary if you need to make sure that something happens independently (for example, disposing of a resource, etc.)

0
source

The big difference is that try ... catch will swallow the exception, hiding the fact that an error has occurred. try..finally will run your cleanup code, and then the exception will continue, handled by knowing what to do with it.

0
source

Source: https://habr.com/ru/post/1401552/


All Articles