O'Reilly logo

PHP 5 Kochbuch, Third Edition by Stephan Schmidt, Ulrich Speidel, Matthias Brusdeylins, Carsten Lucke, Adam Trachtenberg, David Sklar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

9.9 Klassenabhängiges Exception-Handling

Problem

Sie möchten einen Codeblock schützen, in dem verschiedene Klassen von Exceptions auftreten können. Dabei möchten Sie das Exception-Handling von der jeweiligen Klasse abhängig machen.

Lösung

Verwenden Sie mehrere catch-Blöcke:

class EPriceException extends Exception { public $price; // Preis für den catch-Block erhalten. public function __construct($message, $code, $price) { parent::__construct($message, $code); $this->price = $price; } } class EPriceNotANumberException extends EPriceException { public function __construct($price) { parent::__construct("Price is not a number", 1, $price); } } class EPriceNegativeException extends EPriceException { public function __construct($price) { parent::__construct("Price ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required