(PECL rar >= 2.0.0)
RarException::setUsingExceptions — 啟用和停用使用例外處理錯誤
若且唯若參數為 true
時,當 UnRAR 函式庫遇到錯誤時,它會拋出 RarException 類型的例外,而不是發出警告並返回表示錯誤的特殊值。
以下錯誤發生在函式庫外部時(錯誤碼為 -1),也會拋出例外:
無回傳值。
範例 #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