Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

Exception handling - good practise - any advice?

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
afflik1923
Asked:
afflik1923
  • 2
  • 2
1 Solution
 
RoonaanCommented:
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
 
afflik1923Author Commented:
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
 
RoonaanCommented:
On large projects i would look into zend platform. Also look into set_error_handler. File size can be managed with logrotation.
0
 
afflik1923Author Commented:
Thanks for the input
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now