<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: try code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 06 Oct 2008 16:05:00 GMT</pubDate>
    <description>DZone Snippets: try code</description>
    <item>
      <title>Converting old PHP Errors into Exceptions</title>
      <link>http://snippets.dzone.com/posts/show/1617</link>
      <description>The PHP5 Exception class will not work correctly. It would reflect an incorrect line number and file name and not record the 'errcontext' value associated with PHP errors. To solve this problem, we must extend the Exception class.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/**&lt;br /&gt; * This exception behaves like a "old school" PHP Error&lt;br /&gt; */&lt;br /&gt;class STEM_ErrorException extends Exception&lt;br /&gt;{&lt;br /&gt;	/**&lt;br /&gt;	 * The PHP Error Context&lt;br /&gt;	 *&lt;br /&gt;	 * The fifth parameter is optional, errcontext, which is an array that points to the active symbol table at the point the error occurred. In other words, errcontext  will contain an array of every variable that existed in the scope the error was triggered in. User error handler must not modify error context.&lt;br /&gt;	 */&lt;br /&gt;	private $m_arContext;&lt;br /&gt;&lt;br /&gt;	/**&lt;br /&gt;	 * Constructor&lt;br /&gt;	 */&lt;br /&gt;	public function __construct($vMessage, $vCode, $vFile, $vLine, $arContext = null)&lt;br /&gt;	{&lt;br /&gt;		parent::__construct($vMessage, $vCode);&lt;br /&gt;		&lt;br /&gt;		$this-&gt;file = $vFile;&lt;br /&gt;		$this-&gt;line = $vLine;&lt;br /&gt;		&lt;br /&gt;		$this-&gt;m_arContext = $arContext;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;We need to define a function to handle errors. We also want to write two helper functions that will set and restore the error handler to our function. To do this, we write three methods in an abstract class.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/**&lt;br /&gt; * STEM Error Handler&lt;br /&gt; *&lt;br /&gt; * Registers Itself as a PHP Error Handler and proceeds to convert all&lt;br /&gt; * native "old school" PHP errors into new PHP5 Exceptions.&lt;br /&gt; *&lt;br /&gt; * Call STEM_ErrorHandler::Initialize(); before your try blocks and&lt;br /&gt; * STEM_ErrorHandler::Uninitialize(); afterwards.&lt;br /&gt; */&lt;br /&gt;abstract class STEM_ErrorHandler&lt;br /&gt;{&lt;br /&gt;	/**&lt;br /&gt;	 * Encapsulates set_error_handler()&lt;br /&gt;	 */&lt;br /&gt;	public static function Initialize()&lt;br /&gt;	{&lt;br /&gt;		set_error_handler(array("STEM_ErrorHandler", "HandleError"));&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	/**&lt;br /&gt;	 * Encapsulates restore_error_handler()&lt;br /&gt;	 */&lt;br /&gt;	public static function Uninitialize()&lt;br /&gt;	{&lt;br /&gt;		restore_error_handler();&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	/**&lt;br /&gt;	 * Handles PHP Errors&lt;br /&gt;	 */&lt;br /&gt;	public static function HandleError($errno, $errstr, $errfile, $errline, $errcontext)&lt;br /&gt;	{&lt;br /&gt;		throw new STEM_ErrorException($errstr, $errno, $errfile, $errline, $errcontext);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;To test it, we create a file that triggers errors. Before triggering the first error, we call Initialize(). We then Uninitialize() the error handler and trigger the error again to check that our error handler has been removed. It is important to check the line numbers mentioned in the message that this prints!&lt;br /&gt;&lt;code&gt;&lt;br /&gt;STEM_ErrorHandler::Initialize();&lt;br /&gt;&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;	trigger_error("Hello World!");&lt;br /&gt;}&lt;br /&gt;catch (Exception $e)&lt;br /&gt;{&lt;br /&gt;	print $e;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;STEM_ErrorHandler::Uninitialize();&lt;br /&gt;print "&lt;hr /&gt;";&lt;br /&gt;&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;	trigger_error("Hello World!");&lt;br /&gt;}&lt;br /&gt;catch (Exception $e)&lt;br /&gt;{&lt;br /&gt;	print $e;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;Version 0.1.0 - 2006-02-14&lt;br /&gt;STEM: The STEM Cells of PHP&lt;br /&gt;This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License&lt;br /&gt;http://creativecommons.org/licenses/by-sa/2.5/</description>
      <pubDate>Thu, 02 Mar 2006 18:57:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1617</guid>
      <author>Charlie (Stephen Martindale)</author>
    </item>
    <item>
      <title>TryCatch: simple try catch block in perl</title>
      <link>http://snippets.dzone.com/posts/show/928</link>
      <description>&lt;code&gt;&lt;br /&gt;### begin_: file metadata&lt;br /&gt;    ### &lt;region-file_info&gt;&lt;br /&gt;    ### main:&lt;br /&gt;    ###   - name    : TryCatch: try catch block&lt;br /&gt;    ###     desc    : a simple try-catch block example in perl&lt;br /&gt;    ###     date    : created="Thu 2005-12-01 10:58:09"&lt;br /&gt;    ###     last    : lastmod="Thu 2005-12-01 10:58:13"&lt;br /&gt;    ###     tags    : try catch finally error perl cfTryCatch exception&lt;br /&gt;    ### &lt;/region-file_info&gt;&lt;br /&gt;&lt;br /&gt;### begin_: init perl&lt;br /&gt;    use strict;&lt;br /&gt;    use warnings;&lt;br /&gt;&lt;br /&gt;### begin_: try-catch block&lt;br /&gt;    print "begin \n";&lt;br /&gt;    eval{&lt;br /&gt;        ### try block&lt;br /&gt;        print Non_Existent_Function();&lt;br /&gt;    };&lt;br /&gt;    if ($@){&lt;br /&gt;        ### catch block&lt;br /&gt;        print "Failed \n";&lt;br /&gt;    };&lt;br /&gt;    print "end \n";&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 02 Dec 2005 06:00:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/928</guid>
      <author>drefty (drefty)</author>
    </item>
  </channel>
</rss>
