10

I'm very new to php and while I was looking for examples of how to use sockets I noticed that none of them included exception handling code.

First I thought that maybe php didn't have exceptions... but google told me otherwise. There are tons of articles praising the use of exceptions in php (I come from Java & C#, I'm a converted) but then, in the real examples, no one seems to care about trys/catches.

Is it due to the fact that php didn't have exceptions in previous versions?

Example (pseudocode):

$fp = fsockopen($allTheNeededParams);
if(!$fp){
   //error handling
}
fwrite($fp, $out);//<-what if something goes wrong here? the socket is never closed?
fclose($fp);

In other languages, the moment you deal with sockets you see try/catch all over the place. Why not in php?

Two examples from StackOverflow:

Thanks for your time!

6
  • 1
    Because without some additional steps php will not throw an exception on the line you pointed with arrow, but will just output some warning message. + "average" php developer is less experienced than java one. Commented Mar 15, 2011 at 9:41
  • 4
    A lot of PHP libraries do use exceptions, but certainly libraries written for PHP prior to version 5.1.0, or that are intended to be backward compatible with earlier versions, won't to do so. And incredibly, a lot of PHP developers don't understand how to catch and handle exceptions Commented Mar 15, 2011 at 9:43
  • @Mark Baker: even 5.0 i think Commented Mar 15, 2011 at 9:44
  • core components are not allowed to throw exceptions. Commented Mar 15, 2011 at 9:49
  • Your question title is inaccurate, it should be "why doesn't PHP use them built-in?" Commented Mar 15, 2011 at 9:50

2 Answers 2

21

PHP has an apocalypse-themed approach to error handling: if anything goes wrong, the script just ends and any resources it was using (database connections, files, sockets, memory) is freed by the runtime. This makes it less critical to handle such issues than it is in Java or C#, where server code runs continuously — in PHP, an unhandled error condition means a single user is inconvenienced, in Java or C# it might mean a resource leak that ends up bringing the server down for everyone.

And, of course, there's the fact that PHP added exceptions along the way after a huge part of its library of functions was already set in stone, meaning that all those functions use return codes and error reporting instead of exceptions. Replacing errors with exceptions cannot be done at the library level (it would break too much existing code) and at the code level it is an arduous task that involves a lot of boilerplate to detect the error while silencing it, and throw an exception.

Sign up to request clarification or add additional context in comments.

2 Comments

So... regardless of how bad things may go in php, resources will be freed and the server won't break down?
@Gelu: Php scripts can still cause serious problems (hog resources, get stuck in a loop, etc.), but fatal errors (including uncaught exceptions) will halt the script.
0
  1. PHP was not built with exceptions in mind in the first place.
  2. You don't use exceptions for the sake of exceptions. There must be supporting -and object oriented- code. The system needs to be built with exceptions in mind as a whole. And you must know what they do and what they do not.

2 Comments

I wasn't aware of object-oriented code being "exception-friendly" by nature (as illustrated by the need for finally, using or destructors). Care to elaborate?
While you don't have to have object-oriented code in order to use exceptions, there are many points to it. Like exceptions extending exceptions, according to classes that throw them, etc. One can have a look at Zend Framework, Symfony and the likes to see some of it in action. Additionally, see the note at php.net/manual/en/language.exceptions.php. And I presume object oriented code works better with object oriented code in general.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.