Core Function Throw
From Sputnik Wiki
(Difference between revisions)
(→Remarks) |
(→Example) |
||
Line 99: | Line 99: | ||
println("This code gets executed regardless if there was an exception or not"); | 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); | ||
+ | } | ||
+ | say ""; | ||
+ | pause(); | ||
+ | goto begin; | ||
+ | println("All done."); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] |
Revision as of 15:06, 28 August 2013
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
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 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.");