Core Function Throw

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(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...")
 
Line 1: Line 1:
 
<pre>
 
<pre>
Throw( <expression> )
+
Throw <expression>;
 
</pre>
 
</pre>
  
Line 9: Line 9:
 
==== expression ====
 
==== expression ====
  
The string to say the reason why an exception was thrown.
+
An object to send when the exception was thrown.
  
 
=== Return Value ===
 
=== Return Value ===
Line 17: Line 17:
 
=== Remarks ===
 
=== Remarks ===
  
N/A
+
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">
println("Hello Begin");
+
Try
try
+
 
{
 
{
println("Hello Try...");
+
println("This code always executes");
throw("CRASH NOW");
+
// 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
+
Catch ( Exception $e )
 
{
 
{
println("Hello from catch yes there was an error");
+
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
+
Finally
 
{
 
{
println("Hello finally...");
+
println("This code gets executed regardless if there was an exception or not");
 +
}
 +
</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");
 
}
 
}
println("Hello end");
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Revision as of 04:15, 23 January 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

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");
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox