Zephir-异常处理

Zephir异常处理异常处理 #异常处理机制 Zephir能够处理低级的异常,提供跟PHP相似的函数式异常处理方法。 当一个异常被抛出时,须要使用一个catch语句来捕获错误并容许用户自由处理错误。php

try {

    // exceptions can be thrown here
    throw new \Exception("This is an exception");

} catch \Exception, e {

    // handle exception
    echo e->getMessage();
}

Zephir容许用户只使用try来简化错误抛出机制html

try {
    throw new \Exception("This is an exception");
}

若是你不编写任何异常抛出变量,能够这样直接使用:mvc

try {

    // exceptions can be thrown here
    throw new \Exception("This is an exception");

} catch \Exception {
    //不须要定义e直接使用就行,
    // handle exception
    echo e->getMessage();
}

一个catch语句能够捕获多个不一样类型的异常函数

try {

    // exceptions can be thrown here
    throw new \Exception("This is an exception");

} catch RuntimeException|Exception, e {

    // handle exception
    echo e->getMessage();
}

Zephir容许直接抛出静态类型或字符串的异常,其内容会被当作异常的提示信息。code

throw "Test"; // throw new \Exception("Test");
throw 't'; // throw new \Exception((string) 't');
throw 123; // throw new \Exception((string) 123);
throw 123.123; // throw new \Exception((string) 123.123);

Zephir的异常提供了跟PHP同样的异常错误提示。经过Exception::getFile()和Exception::getLine()能够得到异常文件和文件位置。htm

Exception: The static method 'someMethod' doesn't exist on model 'Robots'
File=phalcon/mvc/model.zep Line=4042
#0 /home/scott/test.php(64): Phalcon\Mvc\Model::__callStatic('someMethod', Array)
#1 /home/scott/test.php(64): Robots::someMethod()
#2 {main}
相关文章
相关标签/搜索