Jump to content

Exceptions. When? Why? Where?


Recommended Posts

I have couple question related to Exception class. Big question for me is when to use actually implementation of this class.


1. Do you use Exception for handling ALL errors? For example when user wants to login, do you give him error about wrong username and password (I think this is incorrect) or do you use for example another class which handles validation. For now i use Exception for errors in case database connection failed, when phpass fail to hash password..., basically something that developer can't handle.


3.. Where do you save errors? In txt file, in database? How you do that?

Link to comment
Share on other sites

Exceptions are generally used when something unexpected occurs. The to throw an exception you've defined yourself you must extend the Exception class, if it doesn't you'll get a fatal error. A typical exception you could throw is an InvalidArgumentException which could be used when a variable is of an incorrect type.

  1. Personally I've never used the Exception class on its own as I find it a little to broad and I certainly wouldn't use any exception class for all exceptions.
  2. Where you log errors is entirely up to you. People often used text files.
  3. You can write a logging class which could be dependency injected into any class that needs it.

Link to comment
Share on other sites


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.