Exceptions and logging

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
m4rw3r
Forum Commoner
Posts: 33
Joined: Mon Aug 03, 2009 4:19 pm
Location: Sweden

Re: Exceptions and logging

Post by m4rw3r »

So I should use exceptions completely? or should I make some kind of level on the exceptions to throw?

Like this?

Code: Select all

if(Db::getErrorLevel() & Db::WARNING)
    throw new Exception();
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Exceptions and logging

Post by Eran »

I would handle it by abstracting error handling to a dedicated method / component. That component could, based on configuration, throw an exception or return formatted errors. Then direct all your error handling to that method/component.
User avatar
m4rw3r
Forum Commoner
Posts: 33
Joined: Mon Aug 03, 2009 4:19 pm
Location: Sweden

Re: Exceptions and logging

Post by m4rw3r »

Currently I have several different exception classes, to make it easy to filter them.

So would this be a good solution?

Code: Select all

Db::handleError(new Db_Exception_InvalidConfig($name));
And the handleError() method would then throw the error if the user has configured it to do that, otherwise it will add it to a stack or whatever.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Exceptions and logging

Post by Eran »

Yes, sounds like a good way to handle it.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Exceptions and logging

Post by Ollie Saunders »

BTW you should ignore what I said, I hadn't read the thread properly.
Leave logging to your library users.
Post Reply