Solved

Exception handling - good practise - any advice?

Posted on 2009-04-05
4
223 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 500 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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now