You are on page 1of 3

7/15/13

Try catch finally execution flow - Stack Overflow

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Tell me more

Try catch finally execution flow

If an exception is thrown in catch block of try-catch, then will the finally block be called? t r y { / / s o m et h i n gw h i c ht h r o w se r r o r } c a t c h { / / a g a i ns o m et h i n gt h r o w se r r o r } f i n a l l y { / / f i n a lc l e a nu p } Here, will finally be called? Thanks, Ashwani
try-catch

asked Jan 26 '11 at 16:15 Ashwani K 926 4 19 39 Which language?, just in case... pascal Jan 26 '11 at 16:17

Couldn't you just knock up a little test app in the language and find out..? Sean Jan 26 '11 at 16:18

5 Answers
In at least Java and C#, the f i n a l l y block is always executed regardless how the try block exits. If the answer were false, the f i n a l l y construct would provide no advantage over simply including the code at the end of the try block.
answered Jan 26 '11 at 16:20 Andy Thomas 22.7k 3 26 53

Did you find this question interesting? Try our newsletter


Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

Even though there's an exception or not in the t r y block, f i n a l l y block is bound to execute. That's the way f i n a l l y was designed. If there's an exception in the t r y block, then control will be transferred to the c a t c h block to match the generated exception and will then switch over the control to the f i n a l l y block. stackoverflow.com/questions/4806884/try-catch-finally-execution-flow

1/3

7/15/13

Try catch finally execution flow - Stack Overflow the generated exception and will then switch over the control to the f i n a l l y block.
But in this case, if there isn't any exception generated in the t r y block, the control won't be transferred to the c a t c h block because there wasn't any exception to c a t c h. But as I said earlier, the f i n a l l y block is again bound to execute. So, finally, the finally block will always be executed
answered Feb 19 '11 at 6:45 99tm 15.3k 6 32 67

In every language I know: Yes.


answered Jan 26 '11 at 16:17 Lennart Koopmann 588 2 10

No if there is an exception in both the try and catch blocks as in your example above, control does not pass to the finally block. Illustration:

t r y { / / e x c e p t i o n>c o n t r o lg o e st oc a t c hb l o c k . } c a t c h { / / a g a i ne x c e p t i o n> e x i t ss h o w i n ge r r o r } f i n a l l y { / / c o n t r o ld o e sn o tr e a c hh e r e } NOTE: If however this above code is wrapped inside some function and there is proper exception handling i.e. try... catch block in the calling function, then finally block does get executed and control moves from catch above to the calling function's catch. Illustration: t r y { / / e x c e p t i o n>g o e st oc a t c hb l o c k . } c a t c h { / / a g a i ne x c e p t i o n> g o e st of i n a l l yb l o c k } f i n a l l y { / / c o d eh e r ei se x e c u t e d>g o e st oc a l l i n gf u n c t i o n ' sc a t c hb l o c k }
answered May 23 '12 at 10:16 R.S.K 216 1 16

The answer is yes. But what happens after the Finally block? To illustrate the full flow semantics, consider the following:

stackoverflow.com/questions/4806884/try-catch-finally-execution-flow

2/3

7/15/13

Try catch finally execution flow - Stack Overflow

p r i v a t ei n tT r y C a t c h F i n a l l y F l o w ( ) { i n ti ,j=0 ;/ /s e tjt on o n z e r o t r y { i=1/j ; g o t ol S k i p ;/ /c o m m e n to u t ; r e t u r ni ;/ /c o m m e n to u t l S k i p : ; } c a t c h( E x c e p t i o n ) { i=2 ; t h r o w ;/ /c o m m e n to u t } f i n a l l y { i=3 ; } i=4 ; r e t u r ni ; } p r i v a t ev o i db u t t o n 1 _ C l i c k ( o b j e c ts e n d e r ,E v e n t A r g se ) { i n ti ; t r y {

i=T r y C a t c h F i n a l l y F l o w ( ) ; You can } experiment with j being 0 or not zero, and with commenting out the three "comment out" lines. Step through c a t c h( in E x debug c e p t i mode. o n ) { This will show you fully i= 1 ; how the flows work. }
edited Mar 28 at 14:40

M e s s a g e B o x . S h o w ( i . T o S t r i n g ( ) ) ; }

answered Mar 28 at 14:30 IanC 3,062 2 19 56

Not the answer you're looking for? Browse other questions tagged try-catch or ask your own question.

stackoverflow.com/questions/4806884/try-catch-finally-execution-flow

3/3

You might also like