Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.6 Error Handling

PHP 5 brought exceptions and exception handling to PHP, but it did nothing for catching PHP internal errors. Guess what... we can now catch errors in PHP 7!

2.6 Error Handling

PHP 5 brought the ideas of exceptions and handling those exceptions to PHP. But unfortunately, it did nothing as far as being able to catch the fatal errors that we are all familiar with, like parse error, type error, things like that but thankfully with PHP 7, we now have that ability. So, let's start by creating a variable and let's execute a method on this variable. Now, of course this is not going to work, so whenever we run this code, you're going to see a fatal error, Uncaught error called to member function method on integer blah blah blah blah blah blah. So, this is what's we couldn't do with PHP 5, but we can do with PHP 7. We simply just write this code in a try code block, but whenever we comes to catching this we don't use exception. In fact, let's go ahead and try that and we're going to see that that does not work. So, we're going to echo saying that the error was caught. So, whenever we go back and it definitely did not like that, let say exception it helps if your variable is named correctly, if we go back and run this we get the same thing again. So, here's the thing we have exceptions those typically haven't changed as far as PHP 5 is concerned but then we have errors. So, we have exception handling and then we have error handling. So, if we change this to error then we're going to see that our error is caught. So, we have exceptions, we have errors, and the errors are what we typically would have referred to as internal engine errors or things like that. And we have a variety of different error types. So, we have just the generic error that is going to catch very other type of error, but we also have a type error. So, let's look at an example of that, let's have a function called sum, we'll have an int that is a, an int that is b. And then, we are simply going to return a + b. And then, inside of our try code block, let's comment out this line where we call our fake method. And instead, we are going to call sum, and we're going to pass in to string values or doesn't have to be two string values. Just as long as we have one value that is not of the correct type. Now, if we run this we see that the error was caught, but if we change this to be a specific type error, then only the type of error is going to be caught, in fact let's go ahead. Well, let's not to un-comment that just yet. If we run this again we see that of the errors caught, if we un-comment the code. That generated just a normal error, we are going to see that we have our error message. The reason being because while we are catching the type error, we didn't catch just the generic error of trying to execute this nonexistent method. So, the ideas behind error handling is very much like exception handling. If you want to handle all of your errors you just simply use error. But, if you want to be a little bit more specific and have several different levels of catches if you wanted to do that you could, there's nothing that's going to stop you from doing that, and so there you go. So, as far as the types of errors that you have, you have error which is of course going to be generic you have the type error. You also have a parse error. So, if you are requiring a file that has a parse error, then you can handle that error. There's also an assertion error, so that when ever the call to assert fails, you can handle that there's a arithmetic error. There's also a DivisionByZeroError. And of course all of these errors derive from Error. Otherwise, the ability to catch multiple types of errors and then use error as the final just would not work. So, now let's talk about catching errors and exceptions at the same time. Because we have two different things now, how could we write just code to catch everything? Well, we also have a new type that's simply called Throwable. It is an interface that both exception and error implement. Now, of course I'm only talking about PHP 7 here. So, the exception from PHP 5 is of course not going to implement trouble but exception in PHP 7 does. So let's echo, error caught as throwable. We'll go ahead and comment out these other lines of code. And we'll head on over to the console, we will run this, and we see that the error was caught as throwable. So, if you're writing code that is going to work in either PHP 5 or PHP 7, then you would want to write code like this. The first thing you would want to do is catch the throwable, so that if it is in PHP 7 that this is going to execute. Otherwise, if it's PHP 5 that of course is not going to execute instead it's going to drop down to the exception catch block. So, we now have the ability to catch internal errors, and this is a huge thing because while being able to catch exceptions is important, being able to catch those errors is equally important. The main thing to remember, is that you now have two separate systems essentially, you have exceptions, and you have errors. And if you want to catch both at the same time, you can catch a throwable, or of course you could use any of the more specific types to catch the type of exception or error that you are wanting.

Back to the top