Core Function Throw

From Sputnik Wiki
Revision as of 04:19, 23 January 2013 by UberFoX (Talk | contribs)
Jump to: navigation, search
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");
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox