In this lesson our instructor talks about exceptions. First, he discusses try blocks, catch blocks, and exception class. Then he talks about re-throwing exceptions. He ends the lesson with six helpful homework challenges.
A try block is a section of code enclosed in curly braces that allows what are known as
exceptions to be ‘thrown’ when an error is detected.
An exception is an instance of the built-in PHP Exception class, and the throw
keyword is used to signal that an error/exception has occurred.
When an exception is thrown, execution of code in the try block immediately halts. PHP then looks
for a catch block, which is an error handling section of code enclosed in curly braces, immediately
following the try block where the exception occurred. If a catch block is found, PHP will
run any error handling code contained in the catch block.
try/catch blocks are declared using the try and catch keywords.
When an Exception object is 'caught' it is assigned to a variable that can be used to access the
Exception object between the curly braces of the catch block.
The Exception class constructor can take as its first argument an error message to associate with
the Exception object. This message can be accessed using the Exception class's instance
Once an exception is caught it can be ‘re-thrown’ out of the method it was caught in to be caught
by a try/catch block in a calling method.
Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.