Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Exception handling - good practise - any advice?

Posted on 2009-04-05
4
Medium Priority
?
231 Views
Last Modified: 2013-12-12
Hi,

Despite programming a good few years I'm quite new to exceptions and I'm
writing a general  exception class in my PHP5 / Mysql project that should
there be an error anywhere in the program it is called and an error is
written to an error database.

However some aspects of exceptions still do not fit properly in my head and
I assume this means that I do not understand them properly.


My main gripe is, shouldn't every part of my code be within a try block
because at the end of the day I do not know when an error is going to occur?


The exception class I am going to write is going to be simple. Therefore I
will just extend the default exception class and just add a bit more
information so that I can record what part of the code called the exception
and this info is then written to a MySQL database table.

Now because I don't know where an error could happen in my code (it could
happen anywhere right?)
Do I therefore not just place every single bit of code within every class I
create within a try block.

Therefore my code my will look like this. (Note I'm pretty new to using
classes within PHP5 also)


class example () {


function an_Example1 () {
 try {
  the actual code of the function

 } catch $my_e {
  call the special function to write the error information to the database

 }

}


Now there are two things that come to mind when I write this. Firstly no one
else seems to do this try block around the entire code, so there must be
some fundamental mistake in my understanding.

Also I'm sure the writing of a function to log the error is a good idea and
a commonly used practice. Therefore is there not some example or easy to use
libraries that save me the time of having to write my own.

I welcome any comments on this subject.

Thanks in advance.
0
Comment
Question by:afflik1923
  • 2
  • 2
4 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 2000 total points
ID: 24072044
Hi,

You know that you can use ini_set('error_log', 'somefilename') and ini_set('log_errors','1') to set php to log your errors to a file?

As to the whole Exception story; Overriding the general Exception class is not always a good idea, if your only intent is to implement logging.

I am actually not sure where to start the whole Exception explaining; Nor am I fuly confident that the manuals are less indepth than any explanation I can give you here.

Kind regards

Arnoud
0
 

Author Comment

by:afflik1923
ID: 24072564
OK thanks. For a large project then would you use
ini_set ('error_log', 'filenmd') to log your errors?

Do I need to worry about precautions regarding if the file grows too large due to some error loop?

In a large project what are the basic principles you use for keeping track of your errors?

Thanks
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 24072962
On large projects i would look into zend platform. Also look into set_error_handler. File size can be managed with logrotation.
0
 

Author Closing Comment

by:afflik1923
ID: 31618490
Thanks for the input
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question