Jump to content

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
https://forums.phpfreaks.com/topic/273134-exceptions-when-why-where/
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.

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.