PHP Conference Japan 2024

RarException::setUsingExceptions

(PECL rar >= 2.0.0)

RarException::setUsingExceptions啟用和停用使用例外處理錯誤

說明

public static RarException::setUsingExceptions(bool $using_exceptions): void

若且唯若參數為 true 時,當 UnRAR 函式庫遇到錯誤時,它會拋出 RarException 類型的例外,而不是發出警告並返回表示錯誤的特殊值。

以下錯誤發生在函式庫外部時(錯誤碼為 -1),也會拋出例外:

參數

using_exceptions

應設為 true 以啟用例外拋出,設為 false 則停用(預設值)。

回傳值

無回傳值。

範例

範例 #1 RarException::setUsingExceptions() 範例

<?php
var_dump
(RarException::isUsingExceptions());
$arch = RarArchive::open("does_not_exist.rar");
var_dump($arch);

RarException::setUsingExceptions(true);
var_dump(RarException::isUsingExceptions());
$arch = RarArchive::open("does_not_exist.rar");
var_dump($arch); //不會執行到這裡
?>

上述範例將輸出類似以下的內容

bool(false)

Warning: RarArchive::open(): Failed to open does_not_exist.rar: ERAR_EOPEN (file open error) in C:\php_rar\trunk\tests\test.php on line 3
bool(false)
bool(true)

Fatal error: Uncaught exception 'RarException' with message 'unRAR internal error: Failed to open does_not_exist.rar: ERAR_EOPEN (file open error)' in C:\php_rar\trunk\tests\test.php:8
Stack trace:
#0 C:\php_rar\trunk\tests\test.php(8): RarArchive::open('does_not_exist....')
#1 {main}
  thrown in C:\php_rar\trunk\tests\test.php on line 8

另請參閱

新增註釋

使用者貢獻的註釋

此頁面沒有使用者貢獻的註釋。
To Top