RarException::setUsingExceptions

(PECL rar >= 2.0.0)

RarException::setUsingExceptions例外によるエラー処理の有効化および無効化を行う

説明

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

引数に true を指定すると、UnRAR ライブラリがエラーに遭遇したときの挙動を変更し、 警告を発してエラーを示す値を返すのではなく RarException 型の例外をスローするようにします。

例外は次のエラーのときにもスローされます。これらはライブラリの外部で発生します (エラーコードは -1 です)。

  • 既に閉じた RarArchive オブジェクトやそれに関する RarEntry オブジェクトへのアクセスを試みたとき。
  • 存在しないエントリを RarArchive::getEntry() で取得しようと試みたとき。

パラメータ

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); //not reached
?>

上の例の出力は、 たとえば以下のようになります。

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

参考

関連キーワード:  例外, エラー, RarException, 処理, open, setUsingExceptions, tests, RarArchive, スロー, not