The education of a man is never completed until he dies.—Robert E. Lee (as quoted in Peter's Quotations: Ideas for Our Time (1977) by Laurence J. Peter, p. 175)
Chapter Objectives/Student Learning Outcomes
Explain the difference between errors and exceptions
Create a PHP program that can handle general exceptions
Create a PHP program that can create, raise, and handle user exceptions
Explain and use a switch and/or embedded if/else statement
Create a PHP program that uses the while loop and/or the for loop
Create a program that reads/updates a text file using a two-dimensional array
Create a PHP program that logs exceptions and emails support personnel
Handling Exceptions
As a programmer, you want to do everything possible to ensure that your program will not crash. Anyone using your application will get a bad taste in their mouths if they have to deal with system crashes. You have probably dealt with this situation too. As a user, you may have chosen one application over another because of bad reviews. Once an application has been determined to be “buggy,” it is difficult to convince customers to use the product, even if newer versions have corrected some or all of the problems. An application must be created to handle every possible unanticipated event.
A program must look at each scenario and decide if it can continue or if it must shut down. There will always be a possibility that the application cannot continue to operate due to an unexpected event. Properly developed programs will let the user know that there is a problem without the program crashing. Users are more likely to understand when an application asks them to “try again later” (assuming the problem is fixed before they return to the web site).
Errors are program events that are handled by the system that cause the program to shut down. In some cases, the system can shut down the program and display an error message. Some errors immediately cause the program to crash (such as the server itself crashing). Errors are usually events beyond the control of the program and not directly caused by code (or lack of code) in the program. For example, insufficient memory will cause application errors.
Exceptions are events that are not part of the normal flow of the program logic. All exceptions should be handled by the program. Exceptions can be “raised” when the application anticipates a problem (a missing file) or when the user does something out of the ordinary (tries to enter invalid information). The program should “catch” all the exceptions. It can then examine the exception and determine if it can be corrected, ignored, or if the application must shut down. If a program does not catch exceptions, the system will display the exception message and then shut down the application.
PHP produces a mixture of errors and exceptions depending on the circumstances. Before PHP 5, exception handling did not exist. Thus, some older PHP commands produced errors (which shut down the program) instead of exceptions. Since PHP 7 exception handling is the “rule.” Errors can be handled with exception handling techniques. If exceptions are not handled with program code, the program will halt as if it were a fatal error.
Any time an application is dependent on something external, it is probable that at some point that action will not take place. For example, in the Dog application, the user is expected to enter the proper information. The application must anticipate that not all users will enter correct information. The application is also dependent on several files existing on server (dog_interface, dog_container, dog_applications, and get_breeds). If any of these files are missing, the application cannot continue to function properly.
However, there is a problem with these examples. If you were trying to catch more than one type of exception in the try block, all exceptions would go into the one catch block. Any exception would display the same message. There are a couple of different ways you can handle this.
Programming note—In addition to getMessage method , the Exception and Error objects include
getCode()—Displays the code causing the exception
getFile()—Displays the file name containing code that threw the exception
getLine()—Displays the line number that threw the exception
getTrace() and getTraceAsString()—Display backtrace (exception flow through the program) information
In some circumstances, it might be appropriate to display the Exception or Error message to the users. However, the other methods should only be used for debugging or log entries. Providing code information to the users is usually unnecessary and is a breach of security.
For some, the switch statement is easier to understand when looking at multiple possible values for the same property (variable) or the result of executing a method (as in this example). The default section of the switch statement (or the last else statement in the embedded if statement) catches anything you did not anticipate.
As stated earlier, it is very important that you handle all exceptions and errors. By including the default code, you are able to handle exceptions and errors you may have never anticipated. Notice that each case section must include a break as the last statement. This keeps the code from following through into the next case statement.
Program note—Programmer-created exception classes inherit from Exception. Thus, all the functionality of the Exception class is available from within any new exception class.
Class zeroException extends Exception { }
The previous code creates a valid new zeroException class with no new methods.
catch(zeroException $e) { echo $e->getMessage(); }
This catch block will be called by the new exception and display the exception message generated by the Exception class.
For each exception class that is created and thrown, there must be a catch block to catch the exception. In the example, there are two catch blocks: one catches the zeroException and the other catches any other exceptions that might occur. Just like the previous example using a switch default or if else statement, you should always have the last catch blocks handle any remaining exceptions or errors. If the generic catch block is listed first, all exceptions would be caught by that block and not the specific block for the exception.
As stated, the developer should make every attempt to keep the application from crashing. Errors, however, are designed to display messages and shut down programs with an error code (what you consider to be “crashing” the program). Before PHP 7, in some cases, you could override this functionality by creating a method that will handle errors.
Security and performance—Usually the use of throwing and catching exceptions can reduce the amount of code needed in a program. However, there is a trade-off. Several studies of different object-oriented programming languages have concluded that exception handling is less efficient (performance) than using developer-created routines. The developer should use exceptions as true “exceptions” to the normal flow of the application. For more frequently occurring situations, the developer should create situation handling routines in the application.
In the Dog application, the information flows between many different programs. Each of these programs must be able to handle exceptions properly. However, message handling should all occur in the interface. Any objects that are part of the business rules tier (dog_container, dog, and get_breeds) should pass any exception messages to the interface to handle. At first this may sound like a complex and confusing task. However, the hierarchy of exception handling will greatly simplify this task. As you are about to see, using exception handling will reduce the amount of code necessary.
When exceptions are thrown, the environment will look in the program (or class) itself to determine if there is a catch block that can handle the exception. If there is not a catch block, it will go up one level in the hierarchy and check any calling program (or program that has made an instance of the class) for a catch block. This process will continue until either a catch block has been discovered or it has been determined that the environment itself must handle the exception.
Using this process, we can throw exceptions in the dog_container, dog, and get_breeds modules without using catches. In the dog_interface , we can create a try block around calls to these files. Multiple catch blocks (or one with a switch statement) could be created in the interface to handle the exceptions from both the interface and all the other modules. This satisfies one of the requirements of three-tier programming. The business rules tier (and data tier) passes messages to the interface tier. The interface tier then determines how to handle those messages. It could display them to the users, place them in a log file (which we will look at later in this chapter), or ignore them (if it does not adversely affect the operation of the application).
testerror.php with error and exception-producing methods
The handleerror.php file captures error or exception
The handleerror program (in Example 6-2) includes a method that will handle user errors (errorHandler), along with the set_error_handler command to redirect errors to this method. It also includes a class (userException) that can react when the userException exception is thrown in the try block. The require_once statement is included in the try block in an attempt to capture the error if the file is missing. However, this happens to be a system error (not a user error) which cannot be redirected. To capture system errors, the Error class must be used within a catch block.
After the require_once statement , an instance of class testerror is created. If this class is missing, the system will also error with a fatal message. The block calls the produceerror method, which causes a user error. This error is redirected to the errorHandler, which throws an exception (errorException). The catch block receives the exception and displays the error message. Since exceptions do not shut down the program (like fatal errors), the flow of the program jumps to the first line after all the catch blocks and executes the echo statement (echo "This line will display";). The reaction to the error will cause the program to skip any remaining code in the try block. In this example, the throwexception method call would be ignored.
Program note—try/catch can also include a finally block after all catch blocks. The finally block will execute for all caught exceptions after the associated catch block has executed. PHP allows the finally block to exist without any catch blocks (but the try block must still exist). One of the most common uses of the finally block is to close files and/or databases when an exception has occurred. A program should not close before files and databases have been properly closed. If not closed properly, the data may become corrupt and not be accessible.
Do It
- 1.
Go to the book’s web site and download the files for Example 6-1 and 6-2. Adjust the testerror program to only create an error. Create an additional testexception program (with a testexception class) to throw an exception. Now adjust the handleerror program to create an instance of both programs. The handleerror program should now be able to handle errors or exceptions from either program (class).
Exception and Error Handling vs. If/Else Conditions
The dog class with exception handling
Security and performance—The __toString method “exposes” whatever it returns to any program that makes an instance of the class in which it exists. Using this method to pass error messages might allow a hacker to determine what incorrect information they are sending into a program. In the dog.class example from Chapter 5, __toString passes back the error_message string containing 'TRUE' or 'FALSE' responses. This is more secure than returning error messages. However, by replacing the __toString method with throwing a special exception, you provide even better security. Hackers must now not only know what the error_message means, but they must also know the name of the exception (setException) in order to capture it in their own programs.
The getbreeds.class with exception handling
The dog_container.php file with exception handling
The dog_interface.php file with exception handling
When comparing Example 5-12 to Example 6-6, the amount of code needed to handle exceptions is less than using if/else conditional statements. The logical flow of the program is easier to follow with very few else statements. This occurs because the exceptions thrown from all the files in this application are handled by the catch blocks in dog_interface. The user errors are thrown to a special setException exception. The system errors are captured by the Error catch block. The error_check_dog_app method (in Example 5-12) has been replaced by the setException class. The code in the class is very similar to the code in the error_check_dog_app. The display of individual update messages in the $eMessage string is removed, since this class reacts to user errors, not successful updates. A general print line has been added in the main body of the code to let the users know that all updates have been successful. The try block has been added around all the code in this interface. This helps to capture any problems in any part of this application. Notice that an exception is also thrown if the dog_container file cannot be found.
Only three catch blocks are required for this application. The setException catch block calls the errorMessage method from the setException class, which determines what user errors have occurred. The information is then displayed back to the user. The Throwable catch block handles all other exceptions. It currently displays this information to the user. However, the Exception and Error catch blocks are currently providing the user too much information. It is a violation of security to inform the user what other problems the application may be experiencing. You should just tell them that the system is not currently available and ask them to check back later. Displaying detailed errors is okay when you are testing. However, it is not good for the real world. You will resolve this breach of security in the next section.
For more information on exception handling, visit www.php.net/manual/en/language.errors.php7.php.
Do It
- 1.
Examine the code from this section. Are there any areas in which error checking could have been converted to exception handling? Go to the book’s web site and download the code for this section. Make the potential changes to the existing code to use additional exception handling.
Logging Exceptions
Applications must have an ability to notify systems analysts when problems occur. However, specific messages about errors should not be displayed to the user of the application. The user should be notified that the system is not currently operational. The systems analyst should be notified of the specific problem that has occurred.
Once you have updated and saved the php.ini and/or apache.httpd files, you must reload your Apache server for the changes to take place (see Chapter 1).
The time zone can also be set with program code. The datefmt_set_timezone method can be used for PHP 5.2+. For more information on setting the time zone with program code, visit www.php.net/manual/en/datetime.settimezone.php.
With only the simple one line of code, PHP created the text-based file in the location specified and placed the message in the file.
If you do not have access to these files, you can specify a specific location in the PHP application to send your messages. This ability also allows you to set up multiple application log files. It is common for an application to have informational log files, authentication (login) log files, error log files, and security log files. By separating each type of message, it is easier to scan for a specific type of message in a log file.
A standard text editor (Notepad++ or Notepad) or log-monitoring software (we will create a log reader program later in this chapter) can be used to view the contents of the file.
Security and performance—The location of the log files should reside in a different folder than the application. The folder will need to allow write access for the application. However, it should be secured from read access or write access outside the server itself. Only authorized personnel should have access to the logs.
Note that the constants (USER_ERROR_LOG and ERROR_LOG) must be changed to variables due to the date method creating a possible variable output (different dates). The format would create a file name similar to User_Errors06062020.log or Errors06062020.log.
Security and performance—While it is tempting to inform the associate receiving the email message of the exact problem that has occurred in the application, do not. By default, email is not encrypted. Sending an unencrypted email with detailed information about your application is inviting hackers to corrupt your application. You should, however, provide enough information in the message (such as a date/timestamp and maybe an error number) to help the associate locate the error message(s) in the log file(s).
The first parameter specifies the message of the email. The second parameter informs error_log to email this information. The third parameter provides the “To” email address. The fourth parameter is an extra header field. This field is commonly used to include the subject of the email and the email address that sent the message. The “From” address must be included or the message will not be sent. The “From” address does not, however, need to be an existing address.
For more information on logging errors, visit www.php.net/manual/en/function.error-log.php.
The dog_interface.php file with exception logging and email
At the top of the Example 6-7 code, the constants USER_ERROR_LOG and ERROR_LOG have been created to pinpoint the name and location of the log files. Locating constants that might be subject to change (such as a tax rate) at the top of the code provides easy access for quick changes by a programmer who is charged with supporting the application. As stated previously, the location of the log file must be in a folder that allows application write access. It is recommended that log files be centrally located in a common folder, with other log files, for easy access by data center personnel (or systems analysts).
The other code changes are located in the catch blocks . The setException catch block returns the error message generated by the setException class to the users. This message lets the users know what properties (Name, Breed, Color, and Weight) were not updated. Errors that caused this exception could have come from the user, or by corruption when the information was transmitted from the client machine to the server. These messages only provide information about the requirements of the properties, which the user already should have known. The catch block also writes a similar message to the user error log. A user error is not an urgent error that needs to be addressed by the analyst. However, tracking trends of user problems can provide an indication of possible changes needed to ensure the user has the best experience possible with the application.
The Throwable and Error catch blocks capture all nonuser-generated exceptions. The messages caused by these exceptions might reveal information that would break the security of the application. Therefore, a generic message (such as "The system is currently unavailable. Please try again later.") should be displayed to the user. Detailed information about the exception (error message, file location, coding line that raised the exception) should be placed in the error log for further analysis. Most exceptions caught by these catch blocks will keep the application from running. Therefore, it is important that personnel be informed of the problems occurring. The catch blocks are a good location to send an email to the support personnel to alert them of any problems.
Now that we have built-in exception handling and error handling into the program, we could edit the php.ini file to turn off error reporting to the user. However, we should wait to do this until all development and testing has been completed. Locate the line "display_errors = On" in the php.ini file. If we change this setting to "display_errors = Off", most error messages will not be displayed to the user. This change will not affect any messages sent back by the program to the user via the echo or print methods (including in any catch blocks). This change will give the developer greater control over the type of messages displayed to the users when there are system problems.
Do It
- 1.
Download the code for this section. Create or use an existing HTML page that does not check for user input errors. Run the program entering values for the name, breed, weight, and color, which should cause user errors. Stop the program and open the contents of the user error log file. Did the errors appear in the file? If not, check the security of the folder that contains the log file to make sure that it allows write access to the log file. Once you are satisfied that it has caught user errors, try to cause other system errors to occur. Hint: Change the file names to nonexistent names in the dog application XML file. Check the error log to determine if the errors have been written to the file. Were you able to cause any errors that are not captured by one of the log files? If so, is there a way to capture those errors?
Reading Log and Text Files
The fopen method will also create the file if it does not already exist. The “a” parameter indicates that anything written to the file should be appended. “w” would indicate that any contents in the file would be lost (written over). The fwrite method will then place the string located in the second parameter ($eMessage) into the file indicated by the first parameter ($logFile). $logFile is a pointer that points to the location of the text file. The fclose method closes the text file.
For more information on writing to text files, visit w3schools at www.w3schools.com/php/php_file_create.asp.
The while loop will continue to loop as long as the conditional statement is TRUE. Once the statement is FALSE, the code will exit the loop and jump to the next line of code after the end of the loop. In this example the error_log file is open for read-only (“r”). The while loop looks at the end of file indicator (feof) of the log file to determine if it has reached the end of the file. If feof returns TRUE, the end of the file has been reached. The loop must continue while we have not reached the end of the file. To cause the conditional statement to produce a TRUE, while there are still records to be read, we must reverse the logic and have feof produce TRUE if there are records and FALSE if there are no records. We can do this by using the ! operator. The ! operator is a NOT operator and it reverses the result. A NOT TRUE is FALSE or a NOT FALSE is TRUE. Thus, !feof operator will now produce TRUE when there are more records and FALSE when there are no more records. The loop in combination with the fgets method will display each record in the file. Once each record is displayed, it will close the file using fclose.
For more information on reading text files, visit w3schools at www.w3schools.com/php/php_file_open.asp.
For more information on the while loop, visit w3schools at www.w3schools.com/php/php_looping.asp.
Two-dimensional arrays are a collection of rows of information. Each row has common information in each position (column). In the previous example, all dog names are in position 0, dog breeds are in position 1, dog weights are in position 2, and dog colors are in position 3. This associates directly with the positions in a table.
Sammy | Lab | 18 | Yellow |
---|---|---|---|
Spot | Mixed | 14 | Mixed |
Princess | Chihuahua | 4 | White |
Max | Boxer | 35 | Brown |
In the loop in this example, the explode method breaks the incoming line from the text file via the | character (actually a space, |, and a space). It places each separated string into the $error_Array at the row indicated by the value in $row_Count. The first time through the loop, the first line of the log file is placed in $error_Array[0] (the first row of the array). Because the explode command separated the string, this causes columns to be created for each piece.
This format verifies that each piece of the string has been placed into the proper position in the array.
$row_count is incremented by 1 before the loop continues. This positions the next line of the file to be placed into the next position in the array ($error_Array[1]) if it is the second line of the file). We, of course, do not want to use print_r to display the results to the users (it is not very pretty).
An echo statement is located just before the while loop to open the HTML table. An additional echo statement (echo "<tr>") exists just inside the while loop to create a row of the table.
For more information on the for loop, visit www.w3schools.com/php/php_looping_for.asp.
Also, in the while loop, a for loop has been created to loop through each of the columns of the row. Since we know that there are four columns, the for loop is a good choice. The for loop is used when we know exactly how many times to loop. The first parameter (before the ;) of the for loop initializes the counting variable ($I=0). This variable ($I) is used to count each loop. The second parameter ($I < 3) includes the comparison to determine if the logical flow will stay in the loop. If the comparison is TRUE, the loop will continue. If it is FALSE, the logical flow jumps to the first statement after the loop (echo "</tr>"). The third parameter ($I++) can increment or decrement the counting variable. The for loop helps the programmer to remember to initialize the variable, check the condition, and increment the variable by requiring all the information in one code line.
The echo statement in the for loop uses the $row_Count and $I variables to pull the information from each column in the current row. The first time in the loop, $row_Count will be 0. The echo statement will display the contents of $errorArray[0][0]. As the for loop continues, the contents of $errorArray[0][1], $errorArray[0][2], and $errorArray[0][3] will be displayed. Each value is placed into a cell in the table using the <td> and </td> tags. Once the for loop completes, the flow drops below the loop and closes the row (echo </tr>). Then the row_Count variable is incremented. If there are more rows (more records in the file), the while loop will continue the process with the next row, until there are no more records in the file. Once the flow jumps out of the while loop, the table is closed (echo "</table>"). Then the file is closed.
The while loop now loads the array with the records and keeps a count of the number of items in the array. After the loop ends and the file has been closed, a for loop works through the array in reverse order to echo out the rows in the table. The counter variable $J begins with the total number of rows in the array ($row_Count). One is subtracted from $row_Count before the loop because it is incremented in the while loop after the last record has been retrieved, which makes the count one too many. $J is then decremented ($J--) for each loop until the value is less than zero. The internal for loop (for($I=0;$I<3;$I++)) has not changed, as it must still loop through each column of the rows to display the information.
By loading the records into an array, we can modify them if needed. Let us assume that we want to be able to delete a record from the log. As long as we know the row number that is to be deleted, we can remove that record from the array. Then we can repopulate the file with the remaining records.
The readerrorlog.php file
In Example 6-8, the displayRecords method contains most of the same code previously shown. Extra CSS code has been added to make the display a little more professional. Also, an HTML href link has been included with each record displayed. The link recalls the program, passing the record number that the user wants to delete.
The set of code in the “main section” (the first lines of code that execute) creates a constant ERROR_LOG to define the location and name of the log file. The file is opened and loaded into the array in the same manner as shown previously.
Program note—The header line of the deleteRecords method (function deleteRecord($recordNumber, &$row_Count, &$error_Array)) uses by reference, instead of by value, to allow $row_Count and $error_Array to be updated in the method. By default, parameters passed into a method cannot be changed by the method (by value). The & indicates that the parameter is passed by reference. This allows the value to be changed. deleteRecords can adjust the count of the number of rows in the array ($row_Count) and the information in the array itself ($error_Array).
By value passes the contents (data) that are contained in the parameter into the method. By reference passes the memory address of the parameter into the method, which allows the method to change the value of the parameter in memory.
The deleteRecords method accepts the record number to be deleted as one of its parameters. Any position in the array above this record number is unchanged. Any position below this record must be shifted up one position. For example, if an array has ten positions (0–9) and the fifth position (4) is to be deleted, then positions 5–9 must now become positions 4–8.
In the following example, position $J+1 is placed into position J for any record after the $recordNumber to be deleted:
Programming note—Programs that retrieve data that will be used throughout the program usually retrieve the information in the initial stages of the code and place it in a data structure (array, list, or dataset). As the data is updated in the program, the information in the data structure is updated. When the data processing has been completed, the information is then returned to the original location (text file or database). Updating the text file or database is usually completed as one of the final stages of the program. The process of a user logging out of a program would provide an event to indicate that the updated data should be saved.
Do It
- 1.
Download the code for Example 6-8 from the book’s web site. Add a try catch block to handle any unexpected problems (such as a nonexistent log file). Test and save your changes.
- 2.
Adjust the Example 6-8 code from either #1 or the book’s web site to allow the users to select which log file to read. The program should allow the users to select either the user log file or the general system log file. Test and save your changes.
Chapter Terms
Errors | Exceptions |
---|---|
try/catch Block | $e->getMessage(); |
switch Statement | Embedded if Statement |
Default Code | extends |
Exception Class | Inheritance |
Child Class | Parent Class |
trigger_error | Hierarchy of Exception Handling |
Raising (Throwing) an Exception | Catching an Exception |
Attitude of the Code | Push the Errors |
Pull the Errors | Log File |
Default PHP Error File | error_log |
Timestamp | date.timezone |
Application Log Files | Constants |
Email Alerts | display_errors |
Text File | Sequential File |
Fopen | fwrite |
Pointer | fclose |
Fgets | while Loop |
Feof | ! operator |
EngineException | explode |
Two-Dimensional Array | Row |
Column | Subscript |
Dynamic Arrays | First Row of an Array |
print_f | increment |
decrement | for Loop |
HTML HREF | unset |
Chapter Questions and Projects
- 1.Which of the following is true about PHP program errors?
- a.
They are used for all exceptional situations before PHP 5.0.
- b.
They are handled by the operating system.
- c.
They might be displayed to the users.
- d.
All of these.
- a.
- 2.Which of the following is true about PHP program exceptions?
- a.
They are used for all exceptional situations after PHP 5.0.
- b.
They can be handled by the operating system.
- c.
They might be displayed to the users.
- d.
All of these.
- a.
- 3.The try/catch block is used to do which of the following?
- a.
Capture all errors
- b.
Capture only known errors
- c.
Capture all exceptions
- d.
Capture only known exceptions
- a.
- 4.Inheritance does which of the following?
- a.
Is part of object-oriented programming
- b.
Allows the child class to use all the attributes of its parent class
- c.
Allows the child to create its own methods or properties
- d.
All of these
- a.
- 5.Text files do which of the following?
- a.
Are easy to use in PHP
- b.
Are more secure than using databases
- c.
Are nonsequential
- d.
None of these
- a.
- 6.PHP log files do which of the following?
- a.
Are created by the error_log method
- b.
Are updated by the error_log method
- c.
Should be located in a different directory than the application
- d.
All of these
- a.
- 7.Which of the following describes two-dimensional arrays?
- a.
They are similar to HTML tables. They contain rows and columns.
- b.
They should be avoided at all costs. They are inefficient and difficult to use.
- c.
They require all columns to be loaded with data before rows.
- d.
None of these.
- a.
- 8.Which of the following describes the first position of a two-dimensional array?
- a.
It has a subscript of 0.
- b.
It has a subscript of 1.
- c.
It has a default value.
- d.
None of these.
- a.
- 9.Application log files should include which of the following?
- a.
User log files
- b.
Error log files
- c.
Informational log files
- d.
All of these
- a.
- 10.Emails generated because of program exceptions should do what?
- a.
Include all error information, including the error descriptions
- b.
Include the code lines that caused the error to occur
- c.
Include the date and time the error occurred
- d.
All of these
- a.
- 1.
All exceptions should be displayed to the users.
- 2.
The for loop should be used when you know exactly how many loops will occur.
- 3.
The while loop will continue to loop until the conditional statement becomes true.
- 4.
unset can be used to release a position in an array.
- 5.
All PHP arrays must be declared before being used.
- 6.
A pointer points to the location in memory that an object resides.
- 7.
The ! operator reverses a TRUE result to a FALSE result.
- 8.
print_f can be used to display the contents of an array.
- 9.
Try/Catch blocks should reside in the business rules and data tiers but not the interface tier.
- 10.
Only Exceptions intentionally thrown by program code should be caught.
- 1.
Explain how hierarchy of exception handling works with three-tier applications.
- 2.
What is the difference between an error and an exception?
- 3.
How do you correct the time zone if PHP is providing an incorrect timestamp?
- 4.
How can PHP programmers try to capture errors so they can be treated as if they are exceptions?
- 5.
Why is it important to have multiple log files produced by an application?
- 1.
Adjust the code from project #1 (or #2) from Chapter 4 to include exception handling and logging.
- 2.
Create an application that will register contestants for your favorite game show. Include verification of the data entered using HTML5 and JavaScript. Also validate the data when it is passed to the application. The application should include an interface PHP program (interface tier) and a registration class (business rules tier). The registration class should include exception handling (both user exceptions and program exceptions). The interface program should handle the exceptions using try/catch blocks as shown in this chapter.
- 1.
Update the ABC Computer Parts Warehouse Inventory application to include exception handling. The application should attempt to handle all exceptions, and errors, when possible.
User exceptions should be logged to a user log. All other exceptions should be logged to a system log. If the exception is considered to be extreme (will cause the program to otherwise crash), an email should be sent to the system administrator. Hint: The Try/Catch block should only exist in the interface tier.