473,748 Members | 5,242 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1740
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*******@attgl obal.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...@gmai l.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_pa th = false, $context =
NULL)
{
if ($handle = fopen ($path, $mode, $use_include_pa th, $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_handle r($errno, $errstr, $errfile, $errline){
if($errno & error_reporting ()){
throw new LogException($e rrstr);
}
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
5172
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. From time to time I do something bad -- for example divide by zero or try to grow a float beyond numeric_limits<float>::max() I told MSVC .net 2003 7.1 to "Break into the debugger" for ALL exceptions in
3
2035
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 propagating errors up the call stack? In the past, in my C++ code, I used to throw exceptions for ALL errors, exceptional or not. It seemed like a nice clean method for detecting, handling, and propagating errors. You can find multitudes of articles...
11
1672
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, vbCrLf) With lstStuff For i = 0 To UBound(strMsg)
8
1352
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 very occasionally it will. I can see a number of ways of doing this, but none of them feel aesthetically pleasing: 1.
7
6570
by: iKiLL | last post by:
Hi all I am still pretty new to .Net and C#. I have come from a VB6 Background.
3
1833
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 MessageBox alert to indicate this. For exceptions, I can simply use a catch-all except statement like: try: ... except Exception, error:
2
1855
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 = open('afile', 'r') content = f.read()
1
1405
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
1736
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 and forums? I've been using exceptions now. However, let's come to the point where EDITOR posts a filled FORM (message, article...). He cannot lose his work!
0
8987
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
8826
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
9534
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...
1
9316
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.