473,883 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_handl er("SendErrorRe port");

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 2116

47*********@gma il.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_handl er("SendErrorRe port");

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_handl er() :

<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_WARNI NG, and most of E_STRICT raised in the
file where set_error_handl er() 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_reportin g(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*********@gma il.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_reportin g(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...@gma il.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*********@gma il.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*********@gma il.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 (DatabaseExcept ion $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
661
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 log as well as fire up my email class and generate an email to send me the error. But no matter what I do, I still get the icky looking yellow and white error screens. I’ve turned on customErrors in the web.config but not default URL setting....
13
6628
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
4498
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 runs into problems on the system where it will actually be used, and since I used so little error-trapping it dies very ungracefully. I will of course try to fix whatever is causing the error and add error-trapping to the functions where the...
3
2046
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
3886
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 visible so the document can be printed and/or modified. This all takes place within one form, in which the Word.Application and Word.Document objects are both private form-level variables. Just to be on the safe side I included this piece of code in...
1
1386
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 programming that is all class type programming. I've never created events of any type
3
5201
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 user to select a client and
2
19511
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 will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
4
7613
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 an excel spreadsheet. There are two tables. One is a list of general contacts, and the other is a list of clubs. The clubs contain members who are within the contacts table. When I add a list of new club members from the
0
9932
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9778
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11112
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10730
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10405
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5784
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4602
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.