473,322 Members | 1,806 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,322 software developers and data experts.

Errors to Exceptions

Hello,

I'm late getting to the PHP scene, but I thought I'd start to get into
it. I have a .NET and Ruby background, therefore I'm an OOP guy.

One thing that I haven't figured out to do is turn on Exception
handling for PHP errors. I'm used to exceptions, and noticed that PHP
5 has support for exceptions, so I'd like all errors thrown as
exceptions.

If anyone could help me with that, I'd very much appreciate it.

Thanks,
Mike
Nov 3 '08 #1
4 1701
Cyphos wrote:
Hello,

I'm late getting to the PHP scene, but I thought I'd start to get into
it. I have a .NET and Ruby background, therefore I'm an OOP guy.

One thing that I haven't figured out to do is turn on Exception
handling for PHP errors. I'm used to exceptions, and noticed that PHP
5 has support for exceptions, so I'd like all errors thrown as
exceptions.

If anyone could help me with that, I'd very much appreciate it.

Thanks,
Mike
It depends on what kind of errors you're talking about. Some cannot be
handled at all.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Nov 3 '08 #2
Cyphos escribió:
I'm late getting to the PHP scene, but I thought I'd start to get into
it. I have a .NET and Ruby background, therefore I'm an OOP guy.

One thing that I haven't figured out to do is turn on Exception
handling for PHP errors. I'm used to exceptions, and noticed that PHP
5 has support for exceptions, so I'd like all errors thrown as
exceptions.
I've never tried myself but you can set your own error handler and use a
custom function that throws exceptions:

http://es.php.net/manual/en/language...ions.php#86575

Just be aware that some error types cannot be handled at all.
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Nov 3 '08 #3
On Nov 3, 1:34*pm, Cyphos <mweich...@gmail.comwrote:
Hello,

I'm late getting to the PHP scene, but I thought I'd start to get into
it. I have a .NET and Ruby background, therefore I'm an OOP guy.

One thing that I haven't figured out to do is turn on Exception
handling for PHP errors. I'm used to exceptions, and noticed that PHP
5 has support for exceptions, so I'd like all errors thrown as
exceptions.

If anyone could help me with that, I'd very much appreciate it.

Thanks,
Mike
Exceptions are a relatively new addition to PHP, they didn't exist as
a concept in PHP 4. PHP 4 would instead emit an error message and
continue or abort depending on the error's severity. For backwards
compatibility reasons, any function that existed in PHP 4 still
behaves like this in PHP 5.

You can write wrapper functions yourself for whatever you need to
throw an exception. For example:

function myFopen ($path, $mode, $use_include_path = false, $context =
NULL)
{
if ($handle = fopen ($path, $mode, $use_include_path, $context))
{
return ($handle);
}
else
{
throw new Exception ('Unable to open file ' . $path);
}
}

You can then use your own function where you would use fOpen inside a
try block and put whatever error recovery code you feel you need in
the catch block.

The fopen call inside the function will of course still cause an error
message to be emitted. You can get around this by either turning
error reporting off or by using the @ operator to suppress the error.
Both have their benefits and drawbacks. Turning off error reporting
has no overhead and in a production environment is recommended anyway
because it prevents information that might be useful to hackers being
emitted by malfunctioning scripts. However in a test environment it
turns off all error reporting and hinders debugging as a result. The
@ operator can be used on a per call basis, but it has considerable
overhead and can really slow a PHP script down if used incautiously
(in a loop for example).
Nov 3 '08 #4
Álvaro G. Vicario wrote:
Cyphos escribió:
>I'm late getting to the PHP scene, but I thought I'd start to get into
it. I have a .NET and Ruby background, therefore I'm an OOP guy.

One thing that I haven't figured out to do is turn on Exception
handling for PHP errors. I'm used to exceptions, and noticed that PHP
5 has support for exceptions, so I'd like all errors thrown as
exceptions.

I've never tried myself but you can set your own error handler and use a
custom function that throws exceptions:

http://es.php.net/manual/en/language...ions.php#86575
This is indeed along the lines of what I do, keep in mind to check
wether the specific error type is in error_reporting :)

function my_error_handler($errno, $errstr, $errfile, $errline){
if($errno & error_reporting()){
throw new LogException($errstr);
}
return true;
}

Although, when one develops you could want to see the errors generated
(strict ones etc.), in which case you could alter the function to also
display it and/or log it somewhere.
Just be aware that some error types cannot be handled at all.
Indeed.

Grtz,

Rik
Nov 3 '08 #5

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

Similar topics

13
by: Chris Stankevitz | last post by:
Hi, I have a very large Visual c++ .net 2003 7.1 native c application (approximately 500,000 lines of code). This application is a simulation that frequently works with floating point numbers....
3
by: clintonb | last post by:
Some programmers, and even Microsoft documents, say you should only throw exceptions for exceptional situations. So how are others handling all the other unexceptional errors? How are you...
11
by: Howard Kaikow | last post by:
I'm using the code below, but an error is not getting trapped. Where can such errors occur? In another process? Try SomeCode Catch ex As Exception Dim strMsg() As String = Split(ex.ToString,...
8
by: Simon Willison | last post by:
Hi all, I have an API design question. I'm writing a function that can either succeed or fail. Most of the time the code calling the function won't care about the reason for the failure, but...
7
by: iKiLL | last post by:
Hi all I am still pretty new to .Net and C#. I have come from a VB6 Background.
3
by: tac-tics | last post by:
I have a program which has a GUI front-end which that runs a separate thread to handle all the important stuff. However, if there is a problem with the important stuff, I want the GUI to raise a...
2
by: wink | last post by:
Hello, I would like to know what would be considered the most Pythonic way of handling errors when dealing with files, solutions that seem reasonable using 2.5: ------- try: f =...
1
by: buu | last post by:
how could I set up mz vs 2005 to halt on some errors even if there is an exception handler? (in debug mode)
0
by: WebCM | last post by:
PDO offers 3 modes of displaying errors: - silent mode - no errors - warnings - Warning: - exceptions - Exception: Which of them is the fastest and the most adequate for applications like CMS...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.