Try Catch Finally
Contents |
Try...Catch..Finally
Description
If an exception (a critical error that would normally terminate your program) takes place inside a TRY you can CAPTURE the error and evaulate what went wrong and maybe fix you program from crashing.
Try { statements ... } Catch ( $e ) { statements ... } Finally { statements ... }
Parameters
TRY
This block is used to simply do a normal operation however if anything goes wrong you will instantl drop into the CATCH block.
CATCH
This block is used to capture the error and see what happened and possibly fix your program or work around the error.
If you add ( $e ) after Catch you can use that variable to get detailed information on what went wrong.
FINALLY
Optional; This block will be executed regardless if you have an exception or not.
Remarks
Try...Catch...Finally statements may be nested.
Example
Full example with all features:
println("Hello Begin"); try { println("Hello Try..."); throw("CRASH NOW"); } catch ($e) { println("Hello from catch the error was: '$e[0]' '$e[1]'"); } finally { println("Hello finally..."); } println("Hello end");
This time without the finally
println("Hello Begin"); try { println("Hello Try..."); throw("CRASH NOW"); } catch ($e) { println("Hello from catch the error was: '$e[0]' '$e[1]'"); } println("Hello end");
This time no finally and no catch variable
println("Hello Begin"); try { println("Hello Try..."); throw("CRASH NOW"); } catch { println("Hello from catch yes there was an error"); } println("Hello end");
This time just no catch variable
println("Hello Begin"); try { println("Hello Try..."); throw("CRASH NOW"); } catch { println("Hello from catch yes there was an error"); } finally { println("Hello finally..."); } println("Hello end");