Exceptions versus Errors4:40 with Alena Holligan
Exceptions are an object-oriented approach to programming, while error are the procedural approach. PHP is an exception light language by default, but you can change errors into exceptions when working with object-oriented code. How and when to use exceptions versus errors and how to tell PHP to throw errors as exceptions.
As I mentioned earlier since an exception is an object itself, 0:00 it's really designed to work best with an object oriented approach to programming. 0:05 PHP offers different options for 0:09 error handling when working with procedural code. 0:12 In most cases use errors for 0:15 procedural code and exceptions when working with objects. 0:17 PHP is an exceptioned light language by default 0:20 meaning most situations will give an error, not an exception. 0:25 PHP 7 has added much more in the way of exception handling, so 0:30 pay attention to differences when switching between versions. 0:34 Both PHP 5 and PHP 7 provide a way to turn errors into exceptions. 0:38 If you're writing an object oriented application that uses exceptions, 0:45 you may want to change errors into exceptions so 0:49 that you can handle everything the same way. 0:53 Let's jump back into Work Spaces and try it out. 0:55 We'll start a new file named exceptions.php. 0:58 Add our opening PHP tags. 1:06 And echo the end of file. 1:10 Then we'll simply add an invalid call to a function 1:14 string position without passing a string. 1:18 Let's take a look at this page in a browser. 1:23 We're getting a warning, error, but no exception. 1:28 Let's tell PHP to throw an exception instead. 1:31 We use PHP's built in function set_error_handler 1:37 to define our own function for handling errors. 1:42 We pass in the name of the function we want to use, exception_error_handler. 1:48 Now let's set up that function. 1:57 The set error handler will past five error details as parameters. 2:11 Two are required and the last three are optional. 2:17 The first parameter is an integer denoting the level of error raised. 2:21 We'll call this severity. 2:25 The second parameter is a string containing the error message. 2:27 We'll call this message. 2:33 The third parameter is optional and 2:35 it's a string which contains the file name in which the error was raised. 2:38 We'll call this file. 2:43 The fourth parameter is optional and 2:45 it's an integer containing the line number at which the error was raised. 2:47 We'll call this line. 2:52 Finally, the fifth parameter is also optional and it's an array containing 2:53 every variable that existed in the scope in which the error was triggered. 2:58 That's more than we'll need in most situations, so 3:03 I'm going to leave that one off. 3:06 Now all we need to do is throw a new exception. 3:08 This time we'll use error exception. 3:13 And here we can pass the details of the error. 3:19 First the message then your own error code if you wish. 3:23 I'll just add 0 then severity, File and line. 3:28 Let's refresh the browser. 3:37 Now we see an uncut error exception. 3:41 Your application now throws exceptions as well as errors, but 3:44 you still have to catch them. 3:47 Let's add a try catch block and turn off error display. 3:49 ini_set display_errors 3:52 Off. 4:02 Try our string position, 4:08 catch Exception 4:17 e echo ERROR 4:23 get message. 4:27 Let's refresh the browser once more. 4:32 Now we can see our exception being caught properly. 4:36
You need to sign up for Treehouse in order to download course files.Sign up