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}