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 instanty 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 ( Exception $e ) after Catch you can use that variable to get detailed information on what went wrong.
The word "Exception" in the catch is actually a NAME of a Class you can insert any name you wish however you must inherit the default Exception class to throw your own unique exceptions see examples below.
A catch statement can also be blank just having the word Catch with no () block this type of catch will get executed regardless of what class was triggered.
You can have multiple CATCH statements.
Note - It doesn't 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.
There are SOME exceptions in Sputnik that are core critical and will ignore the try/catch and half the script instantly with the error message.
Example
Example of using an exception
Try { println("This code always executes"); // None of the exceptions params are required // but it helps to at least have a message (First param) // The second param is the error code can be anything you want // The third param is the PARENT inside a class you would use // the $this variable as the parent for obvious reasons throw new Exception("My Message", 777, $parent); println("This code below the exception will not be executed if there was an exception"); } Catch ( Exception $e ) { println("This code only gets executed if an exception happened"); println("Message: " . $e->getMessage()); // Gets the default or new message println("Code: " . $e->getCode()); // Gets the error code if one exists println("Parent: " . $e->getParent()); // Gets the parent if one exists } Finally { println("This code gets executed regardless if there was an exception or not"); }
Example of using an exception with Redo
my $attempt = 0; Try { println("Lets throw..."); throw new Exception("My Message", 777, null); } Catch ( Exception $e ) { println("Exception!!!!! $attempt"); $attempt++; // Begin the whole Try again if // we havent made enough attempts at it if($attempt < 3) redo; } Finally { println("Finally complete"); } // Prints // Lets throw... // Exception!!!!! 0 // Lets throw... // Exception!!!!! 1 // Lets throw... // Exception!!!!! 2 // Finally complete
Example of creating your own Exception class
Class myException extends Exception { my $myParam; Function __Construct($Message, $Code, $myParam, $Parent) { $this->$myParam = $myParam; parent::__construct($Message, $Code, $Parent); // Call the parent (Exception) class } Function myParam() { return $this->$myParam; } }; Try { println("This code always executes"); // None of the exceptions params are required // but it helps to at least have a message (First param) // The second param is the error code can be anything you want // The third param is the PARENT inside a class you would use // the $this variable as the parent for obvious reasons throw new myException("My Message", 777, "my special param", $parent); println("This code below the exception will not be executed if there was an exception"); } Catch ( myException $e ) { println("This code only gets executed if an exception happened"); println("Message: " . $e->getMessage()); // Gets the default or new message println("Code: " . $e->getCode()); // Gets the error code if one exists println("MyParam: " . $e->myParam()); // Gets our new param that was extended to exception println("Parent: " . $e->getParent()); // Gets the parent if one exists } Finally { println("This code gets executed regardless if there was an exception or not"); }
Example of using multiple Catch statements each one triggering ONLY if a specific exception type is given
Class myException extends Exception{}; Class myException2 extends Exception{}; my $i = 0; begin: Try { if($i == 0) { say "Testing exception class name 'Exception'"; throw new Exception("My Message", 1); } if($i == 1) { say "Testing exception class name 'myException'"; throw new myException("My Message", 1); } if($i == 2) { say "Testing exception class name 'myException2'"; throw new myException2("My Message", 1); } } Catch ( Exception $e ) { println("EXCEPTION BRACE"); println("Message: " . $e->getMessage()); // Gets the default or new message println("Code: " . $e->getCode()); // Gets the error code if one exists } Catch ( myException $e ) { println("myException BRACE"); println("Message: " . $e->getMessage()); // Gets the default or new message println("Code: " . $e->getCode()); // Gets the error code if one exists } Catch ( myException2 $e ) { println("myException2 BRACE"); println("Message: " . $e->getMessage()); // Gets the default or new message println("Code: " . $e->getCode()); // Gets the error code if one exists } Finally { println("This code gets executed regardless if there was an exception or not"); } $i++; if($i == 3) { exit(1); } say ""; pause(); goto begin; println("All done.");
The core exception Class
// All exceptions inherit from the CORE exception class // this is the core exception class that Sputnik uses Class Exception { my $Message; my $Code; my $CodeString; my $Parent; Function __Construct($Message = 'Unknown exception', $Code = null, $Parent = null) { $this->$Message = $Message; $this->$Code = $Code; $this->$Parent = $Parent; } Function getMessage() { return $this->$Message; } Function getCode() { return $this->$Code; } Function getParent() { return $this->$Parent; } }