Jump to content

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,'sysadmin@domain.com');
  }
}

 

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,'sysadmin@domain.com');
  }
}

 

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.