2016-06-08 59 views
0

让我们来看看下面的代码:例外的继承在PHP

class customException extends Exception{} 
$a; 
try{ 
    if(!$a) 
     throw new customException("Variable not initialize"); 
    echo $a; 
} 
catch(customException $e){ 
    echo $e->getMessage(); 
} 

Catch块的工作,我们得到的屏幕上的错误文本,但是如果我改变了catch(customException $e)catch(Exception $e)这将是工作......为什么?我们抛出了类customException的例外,为什么它会工作?

解释我请

+0

异常冒泡基本上所有的自定义异常在他们的核心是简单的例外,所以它是有道理的,如果你考虑它。 – Andrew

回答

0

catch(exception $e)确实意味着像“抓$e如果($e instanceof exception)是真实的。

instanceof检查是一类真正的(如customException),所有的父母(如exception)和所有接口该类或任何父母执行