1 00:00:00,640 --> 00:00:04,670 It's best to catch and handle as many exceptions as possible 2 00:00:04,670 --> 00:00:08,600 as soon as they happen, rather than leaving them to bubble up. 3 00:00:08,600 --> 00:00:10,890 As it enhances the user experience and 4 00:00:10,890 --> 00:00:15,260 reduces the likelihood that they will break your application in some way. 5 00:00:15,260 --> 00:00:17,640 Half writing a file for example. 6 00:00:17,640 --> 00:00:21,840 However, sometimes exceptions happen when we're not expecting them. 7 00:00:21,840 --> 00:00:24,180 They are exceptions after all. 8 00:00:24,180 --> 00:00:28,290 Especially now that we've turned errors into exceptions. 9 00:00:28,290 --> 00:00:32,500 Once again, PHP provides a way to handle these exceptions, 10 00:00:32,500 --> 00:00:35,480 by setting a default exception handler. 11 00:00:35,480 --> 00:00:37,600 Let's go back into Work Spaces and set one up. 12 00:00:38,950 --> 00:00:41,376 Let's add another expression that will cause an exception. 13 00:00:41,376 --> 00:00:46,970 Echo 10/0 then let's refresh the browser. 14 00:00:49,498 --> 00:00:52,849 We no longer see our end of file because this new exception is 15 00:00:52,849 --> 00:00:57,050 not caught in handled, so execution of the script stops there. 16 00:00:57,050 --> 00:00:59,146 Let's add the default exception handler. 17 00:01:03,136 --> 00:01:07,790 Setting up a default exception handler is much like setting up an error handler. 18 00:01:07,790 --> 00:01:12,190 Use the set, exception, handler. 19 00:01:14,060 --> 00:01:16,061 And pass in the function you want to use. 20 00:01:17,844 --> 00:01:19,140 Exception_handler. 21 00:01:23,514 --> 00:01:25,756 Then we add the new exception_handler function. 22 00:01:36,083 --> 00:01:40,420 This function needs to accept one parameter, the exception thrown. 23 00:01:40,420 --> 00:01:42,490 Just like with any other caught exception, 24 00:01:42,490 --> 00:01:44,610 we can now do any exception handling we want. 25 00:01:45,780 --> 00:01:51,317 Let's just echo, uncaught exception, 26 00:01:53,938 --> 00:01:55,821 And then our getMessage. 27 00:01:59,639 --> 00:02:00,880 Now let's refresh the page. 28 00:02:03,700 --> 00:02:07,330 This time we see that the exception handler did catch the error. 29 00:02:07,330 --> 00:02:10,830 But it did not allow the script to continue processing. 30 00:02:10,830 --> 00:02:13,610 That's how the uncaught exception handler works. 31 00:02:13,610 --> 00:02:15,830 So it's a good idea to catch exceptions and 32 00:02:15,830 --> 00:02:18,580 not rely on the default exception handler. 33 00:02:18,580 --> 00:02:22,467 Make sure you check the notes associated with this video for further resources. 34 00:02:23,860 --> 00:02:27,020 >> We've covered everything you need to know to get started 35 00:02:27,020 --> 00:02:31,240 implementing exception handling in your object oriented application. 36 00:02:31,240 --> 00:02:34,750 Throwing and catching exceptions gives you robust control 37 00:02:34,750 --> 00:02:37,830 of your application when something goes wrong. 38 00:02:37,830 --> 00:02:40,600 By turning PHP errors into exceptions 39 00:02:40,600 --> 00:02:44,950 you improve communication throughout all parts of your application. 40 00:02:44,950 --> 00:02:48,460 Start using exception handling when accepting data or 41 00:02:48,460 --> 00:02:50,960 communicating with an external source. 42 00:02:50,960 --> 00:02:52,070 Until next time. 43 00:02:52,070 --> 00:02:53,520 Have fun and keep learning.