Core Function Throw
From Sputnik Wiki
(Difference between revisions)
(Created page with "<pre> Throw( <expression> ) </pre> === Description === Throw an exception causing the program to end (Unless the exception is captured in a TRY statement see example). ==== ex...") |
m (1 revision) |
||
(7 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
<pre> | <pre> | ||
− | Throw | + | Throw <expression>; |
</pre> | </pre> | ||
Line 9: | Line 9: | ||
==== expression ==== | ==== expression ==== | ||
− | + | An object to send when the exception was thrown. | |
=== Return Value === | === Return Value === | ||
Line 16: | Line 16: | ||
=== Remarks === | === Remarks === | ||
+ | You can add a Redo; anywhere on a Try,Catch,Finally statements blocks that will instantly start over from the top and begin the try all over again. | ||
− | + | Unlike a normal function throw uses use parentheses example: | |
+ | <pre> | ||
+ | // This is CORRECT | ||
+ | throw new Exception("My error"); | ||
+ | </pre> | ||
+ | |||
+ | <pre> | ||
+ | // This is WRONG | ||
+ | // It will work and its cute but its WRONG | ||
+ | throw(new Exception("My error")); | ||
+ | </pre> | ||
+ | |||
+ | The object should be an Exception class or something that inherits from the Exception class. | ||
=== Example === | === Example === | ||
+ | Example of using an exception | ||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
− | + | Try | |
− | + | ||
{ | { | ||
− | println(" | + | println("This code always executes"); |
− | throw(" | + | // 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(" | + | 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"); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Example of using an exception with Redo | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | 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 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Example of creating your own Exception class | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | 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"); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Example of using multiple Catch statements each one triggering ONLY if a specific exception type is given | ||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | 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); | |
} | } | ||
− | println(" | + | say ""; |
+ | pause(); | ||
+ | goto begin; | ||
+ | println("All done."); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] |
Latest revision as of 12:38, 14 June 2015
Throw <expression>;
Contents |
Description
Throw an exception causing the program to end (Unless the exception is captured in a TRY statement see example).
expression
An object to send when the exception was thrown.
Return Value
None.
Remarks
You can add a Redo; anywhere on a Try,Catch,Finally statements blocks that will instantly start over from the top and begin the try all over again.
Unlike a normal function throw uses use parentheses example:
// This is CORRECT throw new Exception("My error");
// This is WRONG // It will work and its cute but its WRONG throw(new Exception("My error"));
The object should be an Exception class or something that inherits from the Exception class.
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.");