+ 2

Can't understand why we need finally when we can just put this code after try-catch?

14th Apr 2016, 10:20 PM
Artem Leshchev
Artem Leshchev - avatar
4 Respostas
+ 5
Finally is needed when you want that the program do something even if theres an error or not. For example, when working with files it is necessary that close it always, because if not, later when try to open it again will have an error, it will say that the file is in used. So you put the finally to be sure to close it.
29th Apr 2016, 4:18 PM
Jahiron Rodriguez
+ 5
Best programming practice. What happens in try-catch-finally, stays in try-catch-finally. If you open a file and it must be closed the right way is use "finally" for acomplish it
1st May 2016, 12:51 AM
Cristobal Campos
Cristobal Campos - avatar
+ 5
For example: 1) try { int[] x = new int[3]; x[5] = 10; } catch (DivideByZeroException e) { // Some code } Console.Write("Hi"); // Won't run 2) try { int[] x = new int[3]; x[5] = 10; } catch (DivideByZeroException e) { // Some code } finally { Console.Write("Hi"); // Will definitely run }
7th Sep 2016, 3:44 AM
Samuel Neo
Samuel Neo - avatar
+ 1
to make the program continue with execution.
19th Apr 2016, 8:04 PM
Lloyd Mwaluku
Lloyd Mwaluku - avatar