PHP: exceptions vs errors?

Exceptions are thrown - they are intended to be caught. Errors are generally unrecoverable. Lets say for instance - you have a block of code that will insert a row into a database.It is possible that this call fails (duplicate ID) - you will want to have a "Error" which in this case is an "Exception".

When you are inserting these rows, you can do something like this.

Exceptions are thrown - they are intended to be caught. Errors are generally unrecoverable. Lets say for instance - you have a block of code that will insert a row into a database.It is possible that this call fails (duplicate ID) - you will want to have a "Error" which in this case is an "Exception".

When you are inserting these rows, you can do something like this try { $row->insert(); $inserted = true; } catch (Exception $e) { echo "There was an error inserting the row - ". $e->getMessage(); $inserted = false; } echo "Some more stuff"; Program execution will continue - because you 'caught' the exception.An exception will be treated as an error unless it is caught. It will allow you to continue program execution after it fails as well.

I usually set_error_handler to a function that takes the error and throws an exception so that whatever happens i'll just have exceptions to deal with. No more @file_get_contents just nice and neat try/catch. In debug situations I also have an exception handler that outputs an asp.net like page.

I'm posting this on the road but if requested I will post example source later. Edit: Addition as promised, I've cut and pasted some of my code together to make a sample. I've saved the below to file on my workstation, you can see the results here.

_Context; } public function setContext( $value ) { $this->_Context = $value; } public function __construct( $code, $message, $file, $line, $context ) { parent::__construct( $message, $code ); $this->file = $file; $this->line = $line; $this->setContext( $context ); } } /** * Inspire to write perfect code. Everything is an exception, even minor warnings. **/ function error_to_exception( $code, $message, $file, $line, $context ) { throw new ErrorOrWarningException( $code, $message, $file, $line, $context ); } set_error_handler( 'error_to_exception' ); function global_exception_handler( $ex ) { ob_start(); dump_exception( $ex ); $dump = ob_get_clean(); // send email of dump to administrator?... // if we are in debug mode we are allowed to dump exceptions to the browser.

If ( defined( 'DEBUG' ) && DEBUG == true ) { echo $dump; } else // if we are in production we give our visitor a nice message without all the details. { echo file_get_contents( 'static/errors/fatalexception. Html' ); } exit; } function dump_exception( Exception $ex ) { $file = $ex->getFile(); $line = $ex->getLine(); if ( file_exists( $file ) ) { $lines = file( $file ); }?

> getMessage();? > Uncaught getMessage();? > An uncaught was thrown on line of file that prevented further execution of this request.

Where it happened: 0 && $i getTrace() ) ) :? > Stack trace: File Line Class Function Arguments getTrace() as $i => $trace ) :? > "> $arg ) :?

> "> NULL getTraceAsString();?

That would be helpful. Anything to ease the times I'm made to deal with PHP will help. :-) – Jason Baker May 9 '09 at 14:42 Nice code, thanks.

I don't get where the class X comes from though, and what its purpose is? – Alec Jun 9 '09 at 0:00 everything below "set_exception_handler( 'global_exception_handler' );" is just demo, you won't need it, it's just to show what would happen in a normally non-exception error situation. – Kris Jun 9 '09 at 9:48.

One thing to add here is about handling exceptions and errors. For the purpose of the application developer, both errors and exceptions are "bad things" that you want to record to learn about the problems that your application has - so that your customers have a better experience in the long run. So it makes sense to write an error handler that does the same thing as what you do for exceptions - writes a report to a database, emails the admin, etc.In that regard, take a look at markslade.name/Articles.html?a=11 (making PHP handler errors like exceptions).

Thanks for providing the link! – Mike Moore May 27 '10 at 22:35.

Exceptions are thrown intentionally by code using a throw, errors... not so much. Errors come about as a result of something which isn't handled typically. (IO errors, TCP/IP errors, null reference errors).

This is not necessarily true. In many cases errors are checked for and return codes are intentionally sent back as appropriate. In fact, that is the case for every non-object oriented language.

Exceptions are just that, as well, exceptions to the rule. In both cases, something goes wrong, is noticed, and should be handled. PHP file uploading is one example of intentional error handling via return codes - php.net/manual/en/features.file-upload.errors.Php – evan Nov 17 '11 at 11:53.

" There are a lot of good answers about the differences here. I'll just add in something that hasn't yet been talked about - performance. Specifically, this is for the difference between throwing/handling exceptions and handling a return code (either success or some error).

Usually, in php, this means returning false or null, but they can be more detailed such as with file uploading: php.net/manual/en/features.file-upload.e... I've done a few performance runs in different languages/systems. Generally speaking, exception handling is about 10,000x slower than checking for an error return code.So, if it absolutely, positively needs to finish executing before it even started - well, you're out of luck because time travel doesn't exist. Without time travel, return codes are the fastest option available.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions