473,231 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Error Trapping Within Classes

Pretty new to PHP, I recently started learning about error trapping.
As of right now, I include the following into a page in my website:

-------BEGIN PASTE--------
error_reporting(E_ERROR | E_PARSE);
set_error_handler("SendErrorReport");

function SendErrorReport($errorNumber, $errorMessage, $errorFile,
$errorLine, $vars)
{
[code for handling the error, reporting it to the admin, etc.]
}
---------END PASTE---------

This is working great and catching everything I'd want to catch on the
page. However, it's not catching any errors that happen within a
class.

For example, the page that has this code also creates an instance of a
given class. It then calls a function on that class. Within that
function in the class's code, I would mess up a line to test the error
trapping, and it is not trapped with the above code, but instead by the
default error handling.

How should I go about also trapping the errors that occur within the
class? Any advice would be much appreciated, thank you.
-David

Sep 19 '06 #1
9 2081

47*********@gmail.com wrote:
Pretty new to PHP, I recently started learning about error trapping.
As of right now, I include the following into a page in my website:

-------BEGIN PASTE--------
error_reporting(E_ERROR | E_PARSE);
set_error_handler("SendErrorReport");

function SendErrorReport($errorNumber, $errorMessage, $errorFile,
$errorLine, $vars)
{
[code for handling the error, reporting it to the admin, etc.]
}
---------END PASTE---------

This is working great and catching everything I'd want to catch on the
page. However, it's not catching any errors that happen within a
class.

For example, the page that has this code also creates an instance of a
given class. It then calls a function on that class. Within that
function in the class's code, I would mess up a line to test the error
trapping, and it is not trapped with the above code, but instead by the
default error handling.

How should I go about also trapping the errors that occur within the
class? Any advice would be much appreciated, thank you.
-David
What do you mean by "mess up a line" ? Change it to include invalid
syntax? From the PHP manual on set_error_handler() :

<snip>
The following error types cannot be handled with a user defined
function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING,
E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the
file where set_error_handler() is called.
</snip>

Sep 19 '06 #2
What do you mean by "mess up a line" ? Change it to include invalid
syntax?
Ah, I see what you mean. When I "messed up a line" what I tried was a
typo to a function call (on a result set from a mysqli->query call, I'd
normally call fetch_assoc, but instead tried fletch_assoc, just to see
how it would break). It looks like that was outside the scope of the
custom error trapping capabilities.

When I, instead, just change the name of a variable being used, then it
traps a simple "undefined variable" error just fine.

Well, I guess as long as some errors are untrappable, that's fine. On
that note, does my trapping at least give me the full available scope?
(error_reporting(E_ERROR | E_PARSE);)

Also, for the errors outside of the custom error trapping scope (such
as parser errors), the default response from the user's perspective is
to just see a blank page. Is there any way to change that? Or maybe
that's a result of something I changed early on and didn't quite know
that would be an affect of my change?

Basically, I'm just trying to make this website as user-friendly as
possible in terms of handling errors and telling the users everything
is ok (they kind of need their hands held sometimes), as well as
reporting as many errors as possible to me in a more obvious manner,
instead of just putting them in a log file.
-David

Sep 19 '06 #3
Another question just came to me...
>From within the functions in that class, can I manually throw a kind of
custom error? Say, for example, the function runs a SELECT query on a
database and all the code executes just fine, but no records are
returned. For that particular function, records really _should_ be
returned. Otherwise, something is wrong.

Would there be a way, using the trapping I have in place, to throw an
error with a custom message? Because the only other way I can think of
doing it would be to have a function within the class that sends the
error reports that my custom error handler sends and call that function
within the class.

But, not only is it generally bad design to have to re-define the same
thing in multiple places (and, subsequently, change it in multiple
places if it ever needs to be changed), but that would require some
major changes to my code since the error handler also, within itself,
creates an instance of another class that's defined in another file.
Making a long story short, I'd have to pass that object as an argument
to every function in my first class (of which there are many) and
update all function calls throughout the project. (Unless there's a
way to define an instance of the second class within the first class.
But wouldn't that require including the second class's file within the
constructor of the first class? Isn't that... messy?)
-David

Sep 19 '06 #4

47*********@gmail.com wrote:
What do you mean by "mess up a line" ? Change it to include invalid
syntax?

Ah, I see what you mean. When I "messed up a line" what I tried was a
typo to a function call (on a result set from a mysqli->query call, I'd
normally call fetch_assoc, but instead tried fletch_assoc, just to see
how it would break). It looks like that was outside the scope of the
custom error trapping capabilities.

When I, instead, just change the name of a variable being used, then it
traps a simple "undefined variable" error just fine.

Well, I guess as long as some errors are untrappable, that's fine. On
that note, does my trapping at least give me the full available scope?
(error_reporting(E_ERROR | E_PARSE);)
Before the script is run it gets parsed which includes checking the
syntax. If the syntax is invalid, nothing happens -- at all.
>
Also, for the errors outside of the custom error trapping scope (such
as parser errors), the default response from the user's perspective is
to just see a blank page. Is there any way to change that? Or maybe
that's a result of something I changed early on and didn't quite know
that would be an affect of my change?

Basically, I'm just trying to make this website as user-friendly as
possible in terms of handling errors and telling the users everything
is ok (they kind of need their hands held sometimes), as well as
reporting as many errors as possible to me in a more obvious manner,
instead of just putting them in a log file.
Hopefully by the time your code gets into production you won't have any
syntax errors. You did at least try to run the script before putting
it out there, right?

Sep 19 '06 #5

47comput...@gmail.com wrote:
Another question just came to me...
From within the functions in that class, can I manually throw a kind of
custom error? Say, for example, the function runs a SELECT query on a
database and all the code executes just fine, but no records are
returned. For that particular function, records really _should_ be
returned. Otherwise, something is wrong.

Would there be a way, using the trapping I have in place, to throw an
error with a custom message? Because the only other way I can think of
doing it would be to have a function within the class that sends the
error reports that my custom error handler sends and call that function
within the class.
Look at trigger_error()
<http://www.php.net/trigger_error>

But you may really want to be using exceptions for the case described
above.

Sep 19 '06 #6
Hopefully by the time your code gets into production you won't have any
syntax errors. You did at least try to run the script before putting
it out there, right?
Oh, this is all in a testing environment, of course. I'm months away
from anything that can be considered "production" by any means. I'm
just trying to cover all the bases, is all. A parser error is just an
example, but what I'm just trying to ensure is that errors in general
are handled gracefully from the users' perspectives.

In general, I'd rather plan for it and not need it than need it and not
plan for it.
-David

Sep 19 '06 #7
Look at trigger_error()
<http://www.php.net/trigger_error>

But you may really want to be using exceptions for the case described
above.
This has definitely sent me in the right direction, thank you :)

Hopefully one last question: Suppose I enclose my function's code
within a try/catch. When I want to manually error out, I throw a new
Exception with the error message I want. From a design perspective,
what _should_ I be doing in that catch block?

Currently, for my testing, I'm just calling trigger_error in the catch
block to execute my custom error handler which is working like a charm.
Is this bad form in any way? This may be a silly question, but I
figure it's worth asking.
-David

Sep 19 '06 #8

47*********@gmail.com wrote:
Look at trigger_error()
<http://www.php.net/trigger_error>

But you may really want to be using exceptions for the case described
above.

This has definitely sent me in the right direction, thank you :)

Hopefully one last question: Suppose I enclose my function's code
within a try/catch. When I want to manually error out, I throw a new
Exception with the error message I want. From a design perspective,
what _should_ I be doing in that catch block?

Currently, for my testing, I'm just calling trigger_error in the catch
block to execute my custom error handler which is working like a charm.
Is this bad form in any way? This may be a silly question, but I
figure it's worth asking.
-David
The beauty of exceptions is that if you want to handle them you can, or
you can let someone else handle them, or you can not handle them at
all. Furthermore, you can decide what to do with it based on some sort
of condition. So, for example you might say:

try {
//do something that might throw an exception with code = 1
//do something that might throw an exception with code = 2
} catch (Exception $e) {
if($e->getCode() == 1) {
//I will gracefully handle the exception when the code is 1
}
else {
throw $e; //let someone else deal with it
}
}

Sep 19 '06 #9
ZeldorBlat wrote:
47*********@gmail.com wrote:
Look at trigger_error()
<http://www.php.net/trigger_error>
>
But you may really want to be using exceptions for the case described
above.
This has definitely sent me in the right direction, thank you :)
Hopefully one last question: Suppose I enclose my function's code
within a try/catch. When I want to manually error out, I throw a new
Exception with the error message I want. From a design perspective,
what _should_ I be doing in that catch block?

Currently, for my testing, I'm just calling trigger_error in the catch
block to execute my custom error handler which is working like a charm.
Is this bad form in any way? This may be a silly question, but I
figure it's worth asking.
-David

The beauty of exceptions is that if you want to handle them you can, or
you can let someone else handle them, or you can not handle them at
all. Furthermore, you can decide what to do with it based on some sort
of condition. So, for example you might say:

try {
//do something that might throw an exception with code = 1
//do something that might throw an exception with code = 2
} catch (Exception $e) {
if($e->getCode() == 1) {
//I will gracefully handle the exception when the code is 1
}
else {
throw $e; //let someone else deal with it
}
}
Extending upon that example, Exceptions can also give you a greater
granularity as to the TYPE of errors that occur. By inheriting the
base Exception class into your own customized versions, you can have
multiple types of exceptions that are handled differently. So not only
can you decide whether to handle an exception or throw it up the stack,
but you can also handle each type in it's own way. For example, adding
to the quoted example above:

try {
//do something that might throw an exception with code = 1
//do something that might throw an exception with code = 2
} catch (DatabaseException $e) {
//my query returned no results, so I want to handle that right here
} catch (Exception $e) {
//a generic exception occured
if($e->getCode() == 1) {
//I will gracefully handle the exception when the code is 1
}
else {
throw $e; //let someone else deal with it
}
}

Sep 19 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: JP | last post by:
I need to be able to trap errors at the application level. I added this code to the Global.asax file. The code I wrote is supposed to get the last error that was generated and write to the event...
13
by: deko | last post by:
I use this convention frequently: Exit_Here: Exit Sub HandleErr: Select Case Err.Number Case 3163 Resume Next Case 3376 Resume Next
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
3
by: Smriti Dev | last post by:
Hi There, I have the following code and when I try to run it, I get a type mismatch error. I would really appreciate your help with this. Thanks kindly, smriti --- Private Sub cmdOK_Click()
2
by: Captain Nemo | last post by:
I'm still using Office 2000 myself, but some of my clients have Office 2003. I've recently added a piece of code to create an instance of Word, open a document, fill in the blanks and become...
1
by: Elmo Watson | last post by:
Up until now, I've done all the work (gui/front end programming, classes, etc) I've done all my error trapping (try/catch) in the methods in the code behind. Now I've got an opportunity to do...
3
by: Jim Armstrong | last post by:
Hello all - This is driving me crazy. I have a table called tblClients - very simple, has the following fields: taxID (PK) ClientName SalesName The main form of my application allows a...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
4
by: franc sutherland | last post by:
Hello, I am using Access 2003. I am having trouble trapping the "can't append all the records in the append query" error message when appending data to a query from a table which is linked to...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.