By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,559 Members | 1,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,559 IT Pros & Developers. It's quick & easy.

How to trigger an error inside a function and make it come from whereit was called?

P: n/a
function test1() {
trigger_error('My error');
}

application.php

//code...
test1();
//code...

I want the trigger_error to say the error came from application.php
and the line where it came from. I have a custom error handler, so if
some change on the error handler (or test1(), for that matter) is
needed, I will gladly do it.

PHP does it, when you call any PHP function and it errors, it says it
came from the file that called it. In my own functions, it says it
came from the declaration... which is kind of useless because that is
what I already know... but when in the application it happened...
that's what's hard to find out.
Jan 14 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
http://ca.php.net/manual/en/language.exceptions.php

On Jan 14, 3:08 am, Bruno Rafael Moreira de Barros
<brunormbar...@gmail.comwrote:
function test1() {
trigger_error('My error');

}

application.php

//code...
test1();
//code...

I want the trigger_error to say the error came from application.php
and the line where it came from. I have a custom error handler, so if
some change on the error handler (or test1(), for that matter) is
needed, I will gladly do it.

PHP does it, when you call any PHP function and it errors, it says it
came from the file that called it. In my own functions, it says it
came from the declaration... which is kind of useless because that is
what I already know... but when in the application it happened...
that's what's hard to find out.
Jan 14 '08 #2

P: n/a
On Jan 14, 11:17 am, flowover <flowover...@gmail.comwrote:
http://ca.php.net/manual/en/language.exceptions.php

On Jan 14, 3:08 am, Bruno Rafael Moreira de Barros

<brunormbar...@gmail.comwrote:
function test1() {
trigger_error('My error');
}
application.php
//code...
test1();
//code...
I want the trigger_error to say the error came from application.php
and the line where it came from. I have a custom error handler, so if
some change on the error handler (or test1(), for that matter) is
needed, I will gladly do it.
PHP does it, when you call any PHP function and it errors, it says it
came from the file that called it. In my own functions, it says it
came from the declaration... which is kind of useless because that is
what I already know... but when in the application it happened...
that's what's hard to find out.
So I would do:

try {

//code...
test1();
//code...

} catch...

Is that right?
Jan 14 '08 #3

P: n/a
On 14 Jan, 11:08, Bruno Rafael Moreira de Barros
<brunormbar...@gmail.comwrote:
I want the trigger_error to say the error came from application.php
and the line where it came from. I have a custom error handler, so if
some change on the error handler (or test1(), for that matter) is
needed, I will gladly do it.
These *are* the parameters to the customer error handler.

from http://uk.php.net/manual/en/function...or-handler.php

The third parameter is optional, errfile , which contains the
filename that the error was raised in, as a string.
The fourth parameter is optional, errline , which contains the line
number the error was raised at, as an integer.

See also:
http://uk.php.net/debug_backtrace

C.
Jan 14 '08 #4

P: n/a
On Jan 14, 1:56 pm, "C. (http://symcbean.blogspot.com/)"
<colin.mckin...@gmail.comwrote:
On 14 Jan, 11:08, Bruno Rafael Moreira de Barros

<brunormbar...@gmail.comwrote:
I want the trigger_error to say the error came from application.php
and the line where it came from. I have a custom error handler, so if
some change on the error handler (or test1(), for that matter) is
needed, I will gladly do it.

These *are* the parameters to the customer error handler.

fromhttp://uk.php.net/manual/en/function.set-error-handler.php

The third parameter is optional, errfile , which contains the
filename that the error was raised in, as a string.
The fourth parameter is optional, errline , which contains the line
number the error was raised at, as an integer.

See also:http://uk.php.net/debug_backtrace

C.
Yes, but the problem is, the error line and file equal to the ones
that lead you to the declaration of the function...

So if you have:

test1(); // trigger the error
test1(); // trigger the error
test1(); // trigger the error
test1(); // trigger the error
test1(); // trigger the error
test1(); // trigger the error
test1(); // trigger the error

And you check the error log, all the errors will have come from the
same place, from inside test1(). But I tested try...catch and it
worked, and it even solved further problems I had.
Jan 15 '08 #5

P: n/a
On Tue, 15 Jan 2008 14:51:35 +0100, Bruno Rafael Moreira de Barros
<br***********@gmail.comwrote:
On Jan 14, 1:56 pm, "C. (http://symcbean.blogspot.com/)"
<colin.mckin...@gmail.comwrote:
>On 14 Jan, 11:08, Bruno Rafael Moreira de Barros

<brunormbar...@gmail.comwrote:
I want the trigger_error to say the error came from application.php
and the line where it came from. I have a custom error handler, so if
some change on the error handler (or test1(), for that matter) is
needed, I will gladly do it.

These *are* the parameters to the customer error handler.

fromhttp://uk.php.net/manual/en/function.set-error-handler.php

The third parameter is optional, errfile , which contains the
filename that the error was raised in, as a string.
The fourth parameter is optional, errline , which contains the line
number the error was raised at, as an integer.

See also:http://uk.php.net/debug_backtrace

Yes, but the problem is, the error line and file equal to the ones
that lead you to the declaration of the function...
Check the link provided to you, the custom handler could examine the
contents of debug_backtrace(), and optionally provide a whole trace, or it
could 'skip' the last step in the backrace based on the function name.
And you check the error log, all the errors will have come from the
same place, from inside test1(). But I tested try...catch and it
worked, and it even solved further problems I had.
Hmmm, I like Exceptions indeed :).
--
Rik Wasmus
Jan 15 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.