【问题标题】:Continue a script after an exception is thrown PHP抛出异常后继续执行脚本 PHP
【发布时间】:2018-01-09 17:20:11
【问题描述】:

我正在使用 PDFParser 并遇到一个抛出的异常,该异常会破坏我的脚本,即使我将它放在下面的 try/catch 块中也是如此。意思是,异常得到echoed,但"Caught exception: " 部分没有。我以为这正是 try/catch 的用途?

    try {
        $text = $pdf->getText();
    } 
    catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    }

github issue comments 没有解决这个问题,所以我认为 SO 可以提供帮助。

【问题讨论】:

  • 您在命名空间文件中吗?你有别名Exception吗?消息是什么?
  • 是的,它是命名空间的,namespace Smalot\PdfParser;Here's the file。例外是Missing catalog
  • 那应该是catch (\Exception $e)吧?顺便说一句,我说的是你的脚本,而不是他们的代码。
  • 啊,我真笨,这行得通,谢谢!
  • 别担心,很容易错过:)

标签: php exception exception-handling try-catch pdfparser


【解决方案1】:

我也面临同样的问题,即缺少目录致命错误。

我已经尝试过 try、throw 和 catch,现在我没有收到任何缺少目录的致命错误。 下面是我应用 try、throw 和 catch 的代码:

public function getPages()
      {
 try{   
      if (isset($this->dictionary['Catalog'])) {
      // Search for catalog to list pages.
      $id = reset($this->dictionary['Catalog']);

    /** @var Pages $object */
    $object = $this->objects[$id]->get('Pages');
    if (method_exists($object, 'getPages')) {
        $pages = $object->getPages(true);
        return $pages;
    }
}

if (isset($this->dictionary['Pages'])) {
    // Search for pages to list kids.
    $pages = array();

    /** @var Pages[] $objects */
    $objects = $this->getObjectsByType('Pages');
    foreach ($objects as $object) {
        $pages = array_merge($pages, $object->getPages(true));
    }

    return $pages;
}

if (isset($this->dictionary['Page'])) {
    // Search for 'page' (unordered pages).
    $pages = $this->getObjectsByType('Page');

    return array_values($pages);
}

throw new \Exception('Missing catalog.');
}
catch(\Exception $e)
{
$pages = '0';
}
}

祝你好运!!

【讨论】:

    猜你喜欢
    • 2012-04-07
    • 2015-10-31
    • 2021-05-13
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多