PHP Workshop ‹#› PHP Error Handling. PHP Workshop ‹#› Types There are 12 unique error types, which can be grouped into 3 main categories: Informational.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

Error HandlingPHPMay-2007 : [#] PHP Error Handling.
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Topics Introduction Types of Errors Exceptions Exception Handling
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
 It is possible to declare names for object references and not assign object references to them.  Such names literally refer to nothing at all.  It.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
API Design CPSC 315 – Programming Studio Fall 2008 Follows Kernighan and Pike, The Practice of Programming and Joshua Bloch’s Library-Centric Software.
CSCI 215 Web Programming II Debugging & Error Handling.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
ITM 352 © Port,KazmanDebugging - 1 ITM 352 Debugging.
Lecture 8 : PHP Errors & Exceptions UFCFR Advanced Topics in Web Development II 2014/15 SHAPE Hong Kong.
Exceptions COMPSCI 105 S Principles of Computer Science.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
PHP Security.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Form Handling.
Throwing and Catching Exceptions Tran Anh Tuan Edit from Telerik Software Academy
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Chapter 12: Exception Handling
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
WEEK EXCEPTION HANDLING. Syntax Errors Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Dealing with Errors. Error Types Syntax Errors Runtime Errors Logical Errors.
Introduction to Exception Handling and Defensive Programming.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course.
Storing and Retrieving Data
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
PHP Error Handling & Reporting. Error Handling Never allow a default error message or error number returned by the mysql_error() and mysql_errno() functions.
PHP Error Handling Section :I Source: 1.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Firoze Abdur Rakib. Syntax errors, also known as parsing errors, are perhaps the most common kind of error you encounter while you are still learning.
Chapter 8 Error Handling and Debugging. Debugging “When you get frustrated, step away from the computer!”
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
PHP (cont.). 2 Functions Definition – function name(arguments){... } –syntax name as for a variable, but without $ arguments as for variables –to return.
Exception Handling How to handle the runtime errors.
PHP Exception Handling How to handle and create user-defined exceptions Mario Peshev Technical Trainer Software University
Martin Kruliš Who is General Failure and why is he reading my disk? by Martin Kruliš (v1.0)1.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Introduction to Exceptions in Java CS201, SW Development Methods.
Section 3.3 Exceptional Situations. 3.3 Exceptional Situations Exceptional situation Associated with an unusual, sometimes unpredictable event, detectable.
ITM 352 Debugging.
Logger, Assert and Invariants
תיכון אהל שם, רמת גן קורס PHP – הרצאת העשרה
Exception Handling In Text: Chapter 14.
ITM 352 Debugging.
An Introduction to Debugging
Error Handling and Debugging
CMSC 202 Exceptions.
Exception Handling.
PHP Parse error Fatal error Warning Notice Updated : August 23,2012
Presentation transcript:

PHP Workshop ‹#› PHP Error Handling

PHP Workshop ‹#› Types There are 12 unique error types, which can be grouped into 3 main categories: Informational (Notices) Actionable (Warnings) Fatal

PHP Workshop ‹#› Informational Errors Harmless problem, and can be avoided through use of explicit programming. e.g. use of an undefined variable, defining a string without quotes, etc. See class example error1.php

PHP Workshop ‹#› Actionable Errors Indicate that something clearly wrong has happened and that action should be taken. e.g. file not present, database not available, missing function arguments, etc. See class example error2.php

PHP Workshop ‹#› Fatal Errors Something so terrible has happened during execution of your script that further processing simply cannot continue. e.g. parsing error, calling an undefined function, etc. See class example error3.php

PHP Workshop ‹#› Identifying Errors E_STRICT Feature or behaviour is depreciated (PHP5). E_NOTICE Detection of a situation that could indicate a problem, but might be normal. E_USER_NOTICE User triggered notice. E_WARNING Actionable error occurred during execution. E_USER_WARNING User triggered warning. E_COMPILE_WARNING Error occurred during script compilation (unusual) E_CORE_WARNING Error during initialization of the PHP engine. E_ERROR Unrecoverable error in PHP code execution. E_USER_ERROR User triggered fatal error. E_COMPILE_ERROR Critical error occurred while trying to read script. E_CORE_ERROR Occurs if PHP engine cannot startup/etc. E_PARSE Raised during compilation in response to syntax error notice warning fatal

PHP Workshop ‹#› Causing errors It is possible to cause PHP at any point in your script. trigger_error($msg,$type); e.g. … if (!$db_conn) { trigger_error(‘db conn failed’,E_USER_ERROR); } …

PHP Workshop ‹#› PHP Error Handling

PHP Workshop ‹#› Customizing Error Handling Generally, how PHP handles errors is defined by various constants in the installation (php.ini). There are several things you can control in your scripts however..

PHP Workshop ‹#› 1. Set error reporting settings error_reporting($level) This function can be used to control which errors are displayed, and which are simply ignored. The effect only lasts for the duration of the execution of your script.

PHP Workshop ‹#› 1. Set error reporting settings <?php // Turn off all error reporting error_reporting(0); // Report simple running errors error_reporting(E_ERROR | E_WARNING | E_PARSE); // Reporting E_NOTICE can be good too (to report uninitialized // variables or catch variable name misspellings...) error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); // Report all errors except E_NOTICE error_reporting(E_ALL ^ E_NOTICE); // Report ALL PHP errors error_reporting(E_ALL); ?> See class example error4.php

PHP Workshop ‹#› 1. Set error reporting settings Hiding errors is NOT a solution to a problem. It is useful, however, to hide any errors produced on a live server. While developing and debugging code, displaying all errors is highly recommended!

PHP Workshop ‹#› 2. Suppressing Errors The operator can be used to suppress function errors. Any error produced by the function is suppressed and not displayed by PHP regardless of the error reporting setting.

PHP Workshop ‹#› 2. Suppressing Errors $db if (!$db) { trigger_error(‘blah’,E_USER_ERROR); }

PHP Workshop ‹#› 2. Suppressing Errors $db if (!$db) { trigger_error(blah.',E_USER_ERROR); } $db Attempt to connect to database. Suppress error notice if it fails..

PHP Workshop ‹#› 2. Suppressing Errors $db if (!$db) { trigger_error(blah.',E_USER_ERROR); } if (!$db) { trigger_error(‘blah’,E_USER_ERROR); } Since error is suppressed, it must be handled gracefully somewhere else..

PHP Workshop ‹#› 2. Suppressing Errors Error suppression is NOT a solution to a problem. It can be useful to locally define your own error handling mechanisms. If you suppress any errors, you must check for them yourself elsewhere.

PHP Workshop ‹#› 3. Custom Error Handler You can write your own function to handle PHP errors in any way you want. You simply need to write a function with appropriate inputs, then register it in your script as the error handler. The handler function should be able to receive 4 arguments, and return true to indicate it has handled the error…

PHP Workshop ‹#› 3. Custom Error Handler function err_handler( $errcode,$errmsg,$file,$lineno) { echo ‘An error has occurred! ’; echo “file: $file ”; echo “line: $lineno ”; echo “Problem: $errmsg”; return true; }

PHP Workshop ‹#› 3. Custom Error Handler function err_handler( $errcode,$errmsg,$file,$lineno) { echo ‘An error has occurred! ’; echo “file: $file ”; echo “line: $lineno ”; echo “Problem: $errmsg”; return true; } $errcode,$errmsg,$file,$lineno) { The handler must have 4 inputs.. 1.error code 2.error message 3.file where error occurred 4.line at which error occurred

PHP Workshop ‹#› 3. Custom Error Handler function err_handler( $errcode,$errmsg,$file,$lineno) { echo ‘An error has occurred! ’; echo “file: $file ”; echo “line: $lineno ”; echo “Problem: $errmsg”; return true; } echo ‘An error has occurred! ’; echo “file: $file ”; echo “line: $lineno ”; echo “Problem: $errmsg”; Any PHP statements can be executed…

PHP Workshop ‹#› 3. Custom Error Handler function err_handler( $errcode,$errmsg,$file,$lineno) { echo ‘An error has occurred! ’; echo “file: $file ”; echo “line: $lineno ”; echo “Problem: $errmsg”; return true; } return true; Return true to let PHP know that the custom error handler has handled the error OK.

PHP Workshop ‹#› 3. Custom Error Handler The function then needs to be registered as your custom error handler: set_error_handler(‘err_handler’); You can ‘mask’ the custom error handler so it only receives certain types of error. e.g. to register a custom handler just for user triggered errors: set_error_handler(‘err_handler’, E_USER_NOTICE | E_USER_WARNING | E_USER_ERROR);

PHP Workshop ‹#› 3. Custom Error Handler A custom error handler is never passed E_PARSE, E_CORE_ERROR or E_COMPILE_ERROR errors as these are considered too dangerous. Often used in conjunction with a ‘debug’ flag for neat combination of debug and production code display.. See class example error5.php

PHP Workshop ‹#› Review Various different error types exist in PHP. The error handling system is highly flexible, and your own error handling methods can be developed.