Try Catch Finally
(→Example) |
(→Example) |
||
Line 58: | Line 58: | ||
catch ($e) | catch ($e) | ||
{ | { | ||
− | println("Hello from catch the error was: '$e[0]' '$e[1]'"); | + | println("Hello from catch the error was: '$e[0]' '$e[1]' '$e[2]'"); |
} | } | ||
finally | finally | ||
Line 78: | Line 78: | ||
catch ($e) | catch ($e) | ||
{ | { | ||
− | println("Hello from catch the error was: '$e[0]' '$e[1]'"); | + | println("Hello from catch the error was: '$e[0]' '$e[1]' '$e[2]'"); |
} | } | ||
println("Hello end"); | println("Hello end"); |
Revision as of 02:19, 21 January 2013
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.
Note - It doesnt need to be $e you can use $moo if you want however $e or $exception is more readable.
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]' '$e[2]'"); } 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]' '$e[2]'"); } 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");