/    Sign up×
Community /Pin to ProfileBookmark

Error Handling in PHP

I can see errors if I use the command

[code=php]error_reporting(E_ERROR);[/code]

but I can’t seem to catch them with

[code=php]set_error_handler(“customError”, E_ERROR);[/code]

The error handling works with E_USER_ERROR.

Is there anyway to trigger my error handler function on the same errors I see via

[code=php]error_reporting(E_ERROR);[/code]

????

Thanks in advance.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@auxoneauthorJun 03.2008 — Thanks. I like how that's an ASP page about PHP. hah.
Copy linkTweet thisAlerts:
@NogDogJun 03.2008 — Also, note the following from the [url=http://www.php.net/manual/en/function.set-error-handler.php]set_error_handler[/url]() manual page:
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.[/quote]
Copy linkTweet thisAlerts:
@auxoneauthorJun 03.2008 — Considering E_ERROR type errors can't be handled in custom functions, I checked out TryThrowCatch and from what I understand the programmer has to manually 'throw' the exception. It doesn't seem like I can achieve the functionality I am looking for with this method. The error I want to catch is fatal. Once it happens everything halts.

Do you think I could set a Top Level exception handler then, presumably, I can Try certain things I know may be fatal, but not have to actually throw the exception manually. Right? I'm still confused on this one. I hope it doesn't catch warnings too.

If you we're wondering, I am trying to open an image by the trial and error method; trying each imagecreatefrom* function until one works. My problem originated from the fact that E_USER_ERROR doesn't trigger on the fatal errors, while E_ALL triggers on both the fatal and warning but the error sends an email so I can't have warnings included.

Thanks again for all your help, guys.
Copy linkTweet thisAlerts:
@auxoneauthorJun 03.2008 — Oh, and the error I am most interested in is "Fatal error: Allowed memory size of 20971520 bytes exhausted (tried to allocate 10240 bytes)", so I can't Try to open an image then Throw the exception if it didn't work because it's fatal.
Copy linkTweet thisAlerts:
@NogDogJun 03.2008 — You can get a pretty good estimate of the memory required for an image by using getimagesize(), then multiplying the width * height * 3 for the number of bytes that will be needed. Then you could use that number to determine whether or not you want to proceed or reject it as being too large.
Copy linkTweet thisAlerts:
@auxoneauthorJun 03.2008 — So then, there is no real way to catch the error I am interested in by any other method but foreseeing it?
Copy linkTweet thisAlerts:
@NogDogJun 03.2008 — I don't think so. It's one of those Catch 22 things, I think: the error occurs because you've used too much memory, so the script dies, and once it dies it's too late to do anything about it. :rolleyes:
×

Success!

Help @auxone spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...