What is the difference between error and exception in php?

I intend to give you a most unusual discussion of error control.

I built a very good error handler into a language years ago, and though some of the names have changed, the principles of error processing are the same today. I had a custom built multi-tasking OS and had to be able to recover from data errors at all levels with no memory leaks, stack growth or crashes. So what follows is my understanding of how errors and exceptions must operate and how they differ. I will just say I do not have an understanding of how the internals of try catch works, so am guessing to some measure.

The first thing that happens under the covers for error processing is jumping from one program state to another. How is that done? I'll get to that.

Historically, errors are older and simpler, and exceptions are newer and a bit more complex and capable. Errors work fine until you need to bubble them up, which is the equivalent of handing a difficult problem to your supervisor.

Errors can be numbers, like error numbers, and sometimes with one or more associated strings. For example if a file-read error occurs you might be able to report what it is and possibly gracefully fail. (Hay, it's a step up from just crashing like in the old days.)

What is not often said about exceptions is that exceptions are objects layered on a special exception stack. It's like a return stack for program flow, but it holds a return state just for error trys and catches. (I used to call them ePush and ePop, and ?Abort was a conditional throw which would ePop and recover to that level, while Abort was a full die or exit.)

On the bottom of the stack is the information about the initial caller, the object that knows about the state when the outer try was started, which is often when your program was started. On top that, or the next layer on the stack, with up being the children, and down being the parents, is the exception object of the next inner try/catch block.

If you put a try inside a try you are stacking the inner try on top of the outer try. When an error occurs in the inner try and either the inner catch can't handle it or the error is thrown to the outer try, then control is passed to the outer catch block (object) to see if it can handle the error, i.e. your supervisor.

So what this error stack really does is to be able to mark and restore program flow and system state, in other words, it allows a program to not crash the return stack and mess up things for others (data) when things go wrong. So it also saves the state of any other resources like memory allocation pools and so it can clean them up when catch is done. In general this can be a very complicated thing, and that is why exception handling is often slow. In general quite a bit of state needs to go into these exception blocks.

So a try/catch block sort of sets a state to be able to return to if all else gets messed up. It's like a parent. When our lives get messed up we can fall back into our parent's lap and they will make it all right again.

Hope I didn't disappoint you.


Let's discuss the differences between errors and exceptions.

  • Recovering from Error is not possible. The only solution to errors is to terminate the execution. Whereas we can recover from Exception by using either try-catch blocks or throwing an exception back to the caller.
  • You will not be able to handle the Errors using try-catch blocks. Even if you handle them using try-catch blocks, your application will not recover if they happen. On the other hand, Exceptions can be handled using try-catch blocks and can make program flow normally if they happen.
  • Exceptions are related to the application whereas Errors are related to the environment in which application is running.

Example

insert();
      $inserted = true;
      }
   catch (Exception $e)
      {
      echo "There was an error inserting the row - ".$e->getMessage();
      $inserted = false;
      }
      echo "Some more stuff";
?>

Explanation

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.

Example


Explanation

Program execution will stop with PHP Notice: Array to string conversion.

What is the difference between error and exception in php?

Updated on 29-Jun-2020 12:48:40

  • Related Questions & Answers
  • Difference between Exception and Error in Java
  • Difference Between Error and Exception in Java
  • What are the differences between an Exception class and an Error class in Java?
  • Exceptions and Error in PHP 7
  • Differentiate between ADR AND GDR.
  • Differentiate between invoice and bill.
  • Differentiate between investing and trading.
  • Differentiate between company and firm.
  • Differentiate between finance and accounting.
  • Differentiate between EBIT AND EBITDA.
  • Differentiate between revenue and turnover.
  • Differentiate between turnover and profit.
  • Differentiate between piconet and scatternet
  • Differentiate between Arp and BGP
  • Differentiate between offer and quotation

What is difference between error and exception?

1. The error indicates trouble that primarily occurs due to the scarcity of system resources. The exceptions are the issues that can appear at runtime and compile time.

What is exception and error handling in PHP?

With PHP 5 came a new object oriented way of dealing with errors. Exception handling is used to change the normal flow of the code execution if a specified error (exceptional) condition occurs. This condition is called an exception.

What is the exception in PHP?

An exception is an object that describes an error or unexpected behaviour of a PHP script. Exceptions are thrown by many PHP functions and classes. User defined functions and classes can also throw exceptions. Exceptions are a good way to stop a function when it comes across data that it cannot use.

What is difference between error and exception give one example of each?

Some of the examples of errors are system crash error and out of memory error. Errors mostly occur at runtime that's they belong to an unchecked type. Exceptions are the problems which can occur at runtime and compile time. It mainly occurs in the code written by the developers.