不能捕获在PHP中抛出的COM异常


Can't catch COM exception thrown in PHP

我一直在搜索谷歌和堆栈溢出,试图弄清楚这个问题。

我似乎无法捕获异常com_exception ?

public function get($pc){
    try{
        $this->com = new 'COM ("WbemScripting.SWbemLocator");
        $WbemServices = $this->com->ConnectServer($pc, $this->com_path, $this->username, $this->password);
    } catch(com_exception $e){
        return false;
    }
    $WbemServices->Security_->ImpersonationLevel = 3;
    $disks =  $WbemServices->ExecQuery("Select * from Win32_LogicalDisk");
    return $this->format_disk($disks); //Returns disk data in a nice format
}

$this->com是私有变量。我目前正在使用该功能来检索服务器PC在我的网络上的磁盘空间,它工作得很好,但如果输入的PC在网络上不存在,它会抛出异常。

无论我做什么,我总是收到Whoops屏幕:

com_exception
<b>Source:</b> SWbemLocator<br/><b>Description:</b> The RPC server is unavailable.

任何帮助都非常感谢!

我猜当你在PHP的命名空间类你需要反斜杠异常注册:

catch('com_exception $e){

代替:

catch(com_exception $e){

我的问题解决了