5

If I use

try{
    function1();
    function2();
    function3();
}
catch(e:Error){
    function4();
}

and let's say in function2() an exception is thrown, which of the code does get executed by definition? will function3() get executed? will the effects of function1 be present after the catch? (there are programming languages that 'rewind' the effects such as if the whole block wasn't executed)

thanks for clarification!

Mat
  • 567
  • 8
  • 24

1 Answers1

9

A try catch will execute all code UNTIL an exception is thrown. At that point, the exception will bubble until it either hits a catch block or the program exits. Flash does not "rewind" any code it has executed.

Say function2() is 10 lines and line 4 throws the exception, lines 5-10 will not be executed. Nor will function3(). The code will go into your catch and then execute function4().

Another construct for use in try..catch.. is the finally block, which is a section of code that gets executed after the try or catch. It is particually useful for things like myNetConn = null where you may have had an error closing a NetConnection, but still wish to null the object.

Ben Roux
  • 7,308
  • 1
  • 18
  • 21
  • 1
    thank you! if finally is executed nevertheless, whats the difference of putting the code just after the catch clause instead of putting it into 'finally'? – Mat Jun 14 '11 at 15:07
  • 2
    http://stackoverflow.com/questions/547791/why-use-finally-in-c Has a very good answer for this. The general answer is the `finally` will executed even if your `catch` code throws an exception itself. – Ben Roux Jun 14 '11 at 15:13