<?php
class Seller {
public $cars = [];
public function sell() {
if (count($this->cars) == 0) {
throw new Exception("No cars for sale");
}
//具体实现……
}
}
$seller = new Seller();
try {
$seller->sell(); //<-Error
}
catch (Exception $e) {
echo $e->getMessage();
//e->getMessage() is "No cars for sale"
}