Jump to content

[SOLVED] Custom Exceptions, using multiple catch?


ifubad

Recommended Posts

When using multiple catch for custom exceptions, as of now, I have the custom exception classes as separate include files. Like so

 

Catch block

catch(FileException $e){
  $e->showError();
}
catch(MailException $e){
  $e->logError();
}

 

custom Exception classes

 

Filename: FileException.inc.php

class FileException extends Exception{
  public function showError(){
    die($this->getMessage());
  }
}

 

Filename: MailException.inc.php

class MailException extends Exception{
  public function logError(){
    error_log($this->getMessage(),1,'[email protected]');
  }
}

 

Is it possible to keep the different custom exceptions in a single/central file? If so, can you provided a simple example on how to implement. Or, is it that when using multiple custom exceptions, they HAVE to be kept in separate files?

 

TIA

You would simply define your exception classes within the one file. eg;

 

exceptions.php

<?php

class FileException extends Exception{
  public function showError(){
    die($this->getMessage());
  }
}

class MailException extends Exception{
  public function logError(){
    error_log($this->getMessage(),1,'[email protected]');
  }
}

 

However, this makes things more difficult when using any autoloading mechanism (as you may be), in that case its best to store your exceptions within there own files.

You would simply define your exception classes within the one file. eg;

 

Thank you, that part I do know. What I was wondering is that if it was possible to store different exception classes in a single "include" file. That way, I figured it MAY make it easier to maintain all of the custom exceptions.

However, this makes things more difficult when using any autoloading mechanism (as you may be), in that case its best to store your exceptions within there own files.

 

cool, tried it and I understood your last reply. Thanks

 

What do you mean by autoloading mechanism and how does it make it more difficult?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.