为类创建一个简单的异常

时间:2014-01-20 18:11:26

标签: php oop exception exception-handling

我正在创建一个名为ex:foo

的类
class foo{

 function __construct($arg){
    if(empty($arg)){
      throw new fooException('argument can not be empty');
    }
 }

}

我尝试创建类而不通过像

这样的构造函数传递任何东西
try{
$o = new foo();
}catch(FooException $e){
 echo $e->getMessage(); 
}

我收到错误Fatal error: Class 'FooException' not found in .. 我意识到,我需要扩展错误异常类,但是没有关于如何在SO中执行此操作的示例。

2 个答案:

答案 0 :(得分:1)

以下是定义名为Exception的自定义FooException的示例。

跟随它的类TestException测试此自定义异常。

<?php
/**
 * Define a custom exception class
 */
class FooException extends Exception
{
    // Redefine the exception so message isn't optional
    public function __construct($message, $code = 0, Exception $previous = null) {
        // some code

        // make sure everything is assigned properly
        parent::__construct($message, $code, $previous);
    }

    // custom string representation of object
    public function __toString() {
        return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
    }

    public function customFunction() {
        echo "A custom function for this type of exception\n";
    }
}

/**
 * Create a class to test the exception
 */
class TestException
{
    public $var;

    const THROW_NONE    = 0;
    const THROW_CUSTOM  = 1;
    const THROW_DEFAULT = 2;

    function __construct($avalue = self::THROW_NONE) {

        switch ($avalue) {
            case self::THROW_CUSTOM:
                // throw custom exception
                throw new FooException('1 is an invalid parameter', 5);
                break;

            case self::THROW_DEFAULT:
                // throw default one.
                throw new Exception('2 is not allowed as a parameter', 6);
                break;

            default: 
                // No exception, object will be created.
                $this->var = $avalue;
                break;
        }
    }
}


// Example 1
try {
    $o = new TestException(TestException::THROW_CUSTOM);
} catch (FooException $e) {      // Will be caught
    echo "Caught my exception\n", $e;
    $e->customFunction();
} catch (Exception $e) {        // Skipped
    echo "Caught Default Exception\n", $e;
}

// Continue execution
var_dump($o); // Null
echo "\n\n";


// Example 2
try {
    $o = new TestException(TestException::THROW_DEFAULT);
} catch (FooException $e) {      // Doesn't match this type
    echo "Caught my exception\n", $e;
    $e->customFunction();
} catch (Exception $e) {        // Will be caught
    echo "Caught Default Exception\n", $e;
}

// Continue execution
var_dump($o); // Null
echo "\n\n";


// Example 3
try {
    $o = new TestException(TestException::THROW_CUSTOM);
} catch (Exception $e) {        // Will be caught
    echo "Default Exception caught\n", $e;
}

// Continue execution
var_dump($o); // Null
echo "\n\n";


// Example 4
try {
    $o = new TestException();
} catch (Exception $e) {        // Skipped, no exception
    echo "Default Exception caught\n", $e;
}

// Continue execution
var_dump($o); // TestException
echo "\n\n";
?>

来源:http://www.php.net/manual/en/language.exceptions.extending.php 我为您的FooException

修改了它

答案 1 :(得分:0)

我在这里看到两件事,首先,你抛出的异常被称为“fooException”,然后,你正在捕捉的那个被称为“FooException”,这是不正确的,也许是一个错字?另一件事是,您是否创建了一个名为“FooException”的类并将其加载到您的应用程序中?

require_once(path/to/FooExceptionClass.php);

希望这有帮助。