有些尝试用php捕获问题

时间:2014-11-05 06:16:00

标签: php

我试图在php中使用crontab创建一个类。 我使用了this tutorial.

我已经安装了libssh2,但是你可以看到它还没有用。所以我的服务器上有一个文件Ssh2_crontab_manager.php。这是它的内容:

<?php
      Class Ssh2_crontab_manager 
      {
            private $connection;
            private $path;
            private $handle;
            private $cron_file;
            function __construct($host=NULL, $port=NULL, $username=NULL, $password=NULL)
            {
                $path_length    = strrpos(__FILE__, "/");
                $this->path      = substr(__FILE__, 0, $path_length) . '/';
                $this->handle    = 'crontab.txt';
                $this->cron_file = "{$this->path}{$this->handle}";

                /*try
                {
                    if ((is_null($host)) || (is_null($port)) || (is_null($username)) || (is_null($password))) throw new Exception("Please specify the host, port, username and password!");
                }
                catch
                {

                }*/
            }


      }
?>

这里是noReplyCrontab.php,我尝试使用这个类:

<?php
include './lib/Ssh2_crontab_manager.php';
//$crontab = new Ssh2_crontab_manager('host', '22', 'user', 'pass');
echo 'WORKS';
?>

如果我现在运行它,它说&#39;工作&#39;,但如果我取消注释try / catch块它只显示白屏,所以我认为有一些错误。任何人都可以向我展示它吗?

2 个答案:

答案 0 :(得分:3)

您的代码说

 catch
 {

 }

但是catch什么?

您必须将该值提供给catch子句

catch (Exception $e)
{
         //now it will work fine
}

<强> Manual

答案 1 :(得分:1)

试试这个

try
{
    if (true) throw new Exception("Please specify the host, port, username and password!");
}
catch(Exception $e)
{
    echo $e->getMessage();
}
相关问题