Max Plössl: Exceptions in PHP5

Beitrag lesen

Hallo @all!

Ich hab da mal eine frage: Welche Vorteile haben Exceptions im Gegensatz zu normaler Fehlerbehandlung. Beispiel:

class Klasse // $this->file kommt vom Konstruktor
{
  public function Handle()
  {
    $this->handle = fopen($this->file, 'r');
    if(!$this->handle)
    {
      throw new Exception('Kein Handle');
    }
  }
}

$a = new Klasse('gibtsnicht.txt');

try
{
  $a->Handle();
}
catch(Exception $e) { echo $e->getMessage() }

--------------------------------------------------------------------

Im Gegensatz zu:

class Klasse // $this->file kommt vom Konstruktor
{
  public function Handle()
  {
    $this->handle = fopen($this->file, 'r');
    if(!$this->handle)
    {
      return 0;  // 0 als Anzeige für Fehler, sonst 1
    }
    else
    {
      return 1
    }
  }
}

$a = new Klasse('gibtsnicht.txt');

if(!$a->Handle()) echo 'Kein Handle';

was ist jetzt also besser, was sollte man verwenden?

MfG, Max