<?php class myException extends Exception { public function __construct($message) { if ($message == 'specialError') { throw new Exception('Opps: Special Error'); } else { throw new Exception('Opps: Generic Error'); } } } /* * Modo Errado try { $e = new myException('desconhecido'); } catch (myException $e) { echo 'Error: ' . $e->getMessage(); //Fatal error: Uncaught exception 'Exception' with message 'Opps: Generic Error' } */ //Modo Correto try { $e = new myException('desconhecido'); } catch (Exception $e) { // <= Aqui está diferente echo 'Error: ' . $e->getMessage(); //Fatal error: Uncaught exception 'Exception' with message 'Opps: Generic Error' }
* This file is part of the symfony package. * (c) 2004-2006 Fabien Potencier <*****@*****.**> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ require_once dirname(__FILE__) . '/../../bootstrap/unit.php'; $t = new lime_test(24, new lime_output_color()); foreach (array('cache', 'configuration', 'controller', 'database', 'error404', 'factory', 'file', 'filter', 'forward', 'initialization', 'parse', 'render', 'security', 'stop', 'storage', 'view') as $class) { $class = sprintf('sf%sException', ucfirst($class)); $e = new $class(); $t->ok($e instanceof sfException, sprintf('"%s" inherits from sfException', $class)); } class myException extends sfException { public static function formatArgsTest($args, $single = false, $format = 'html') { return parent::formatArgs($args, $single, $format); } } // sfException::formatArgs() $t->diag('sfException::formatArgs()'); $t->is(myException::formatArgsTest('foo', true), "'foo'", 'formatArgs() can format a single argument'); $t->is(myException::formatArgsTest(array('foo', 'bar')), "'foo', 'bar'", 'formatArgs() can format an array of arguments'); $t->is(myException::formatArgsTest(new stdClass(), true), "<em>object</em>('stdClass')", 'formatArgs() can format an objet instance'); $t->is(myException::formatArgsTest(null, true), "<em>null</em>", 'formatArgs() can format a null'); $t->is(myException::formatArgsTest(100, true), "100", 'formatArgs() can format an integer'); $t->is(myException::formatArgsTest(array('foo' => new stdClass(), 'bar' => 2), true), "<em>array</em>('foo' => <em>object</em>('stdClass'), 'bar' => 2)", 'formatArgs() can format a nested array'); $t->is(myException::formatArgsTest('&', true), "'&'", 'formatArgs() escapes strings'); $t->is(myException::formatArgsTest(array('&' => '&'), true), "<em>array</em>('&' => '&')", 'formatArgs() escapes strings for keys and values in arrays');
public static function jumpHeader($param = array()) { $headerCode = isset($param['code']) ? $param['code'] : 302; $url = isset($param['url']) ? $param['url'] : ''; switch ($headerCode) { case 302: if ($url) { header('Location: ' . $url); } else { $exception = new myException('跳转地址未设置,请加入参数url'); $exception->echoException(); } break; case 301: if ($url) { header('HTTP/1.1 301 Moved Permanently'); header('Location: ' . $url); } else { $exception = new myException('跳转地址未设置,请加入参数url'); $exception->echoException(); } break; case 404: header('HTTP/1.1 404 Not Found'); break; } exit; }