我不太清楚异常是如何工作的。我认为,它们应该避免PHP错误并显示“我的错误消息”。例如,我想打开文件。
class File{
public $file;
public function __construct($file)
{
try{
$this->file = fopen($file,'r');
}
catch(Exception $e){
echo "some error" . $e->getMessage();
}
}
}
$file = new File('/var/www/html/OOP/texts.txt');
这段代码涉及到IT技术。现在我故意将文件名texts.txt
更改为tex.txt
,只是为了从catch块中看到错误消息,但是PHP却给出了一个错误:Warning: fopen(/var/www/html/OOP/texts.txt): failed to open stream: No such file or directory in /var/www/html/OOP/file.php on line 169
。所以这是PHP的错误,它没有显示来自catch块的错误消息。我做错了什么?try/catch究竟如何工作?